{
// Documentations meter definitions: https://www.cfos-emobility.de/de/cfos-charging-manager/documentation/user-defined-meters.htm
// Documentation Storage controls: https://www.cfos-emobility.de/de/cfos-charging-manager/documentation/battery-rules.htm
// We DO NOT use relative path', even though it is defined in documentation; It won't work otherwise and (examples include localhost, too
// IMPORTANT: Use "discharge_power_w" instead of wrong "discharg_power_w" from docu; This is a TYPO!

	"rtype": 0,								// 0 = modbus, 1 = http/json
	"mtype": 4,								// 0 = other, 1 = meter, 2 = inverter, 4 = battery

	"name": "Marstek Venus - PREPROD 2",	// as displayed in the config dialog
	"manufacturer": "Marstek",
	"dev_type": "marstek_e30_preprod2",		// please change! must be a unique name

// modbus parameters
	"rtu": {
		"silence_period": 50,				// msec
		"retries": 4,
		"rcv_timeout": 500,					// msec
		"silence_same_slave": true			//testweise auf true geändert!
	},
// Spezifische TCP-Parameter hinzufügen (Wichtig laut cFos Doku!)
	"tcp": {
		"connect_timeout": 4000,			// Wartezeit auf TCP-Verbindungsaufbau
		"delay_after_connect": 500			// Kurze Atempause nach Verbindungsaufbau vor erstem Befehl
	},
	"modbus_read": 3,						// function code to read registers
	"modbus_read_max_registers": 60,
	"modbus_write": 6,						// function code to write register(s)
	"modbus_write_max_registers": 48,
	"modbus_allow_gaps": false,				// Verhindert das Mitlesen leerer Register dazwischen
	"delay_accumulated": true,				// kWh-Werte (Energieerzeugung) seltener abfragen
	"upd_delay": 3000,						// Abfrage-Intervall auf 3 Sekunden hochsetzen

// values used to build the model string
	"type_designation": {					// Combined "type_designation" and "version".Would return as "0" if not added; maybe test to use to return other interesting registers later
		"once": true,
		"address": 0,
		"type": "string",
		"fixed": "Marstek Venus E 3.0"
	},
	"version": {							// Would return as "0" if not added; maybe test to use to return other interesting registers later
		"once": true,
		"address": 0,
		"type": "string",
		"fixed": "Version-undef"
	},
	"serial": {								// Would return as "0" if not added; maybe test to use to return other interesting registers later
		"once": true,
		"type": "string",
		"fixed": "Serial-undef"
	},
	"firmware_version": {					// Tried to add proper formating w/o success ...
		"once": true,
		"address": 30200,
		"type": "uint16"
		//FORMAT scheint nicht zu gehen
		//"format": "EMS-V%d"
	},
	"*temperature": {						// Tried to add proper formating w/o success ...
		"address": 35000,
		"type": "int16",
		"resolution": 0.1
		//FORMAT scheint nicht zu gehen
		//"format": "%3.1f °C",
	},
// instantaneous values
	"power_w": {
		"address": 30006,
		"type": "int16",
		"order": "hl",
		"resolution": -1.0
	},
	"voltage_l1": {
		"address": 32200,
		"type": "uint16",
		"order": "hl",
		"resolution": 0.1
	},
	"current_l1": {
		"address": 37004,
		"type": "int16",
		"order": "hl",
		"resolution": -4.0
	},
	"soc": {
		"address": 34002,
		"type": "uint16",
		"order": "hl",
		"resolution": 0.1
	},
	"import_wh": {
		"address": 33000,
		"type": "uint32",
		"order": "hl",
		"resolution": 10
	},
	"export_wh": {
		"address": 33002,
		"type": "uint32",
		"order": "hl",
		"resolution": 10
	},
// Reads current status of manual mode; Helper so we can write manual mode only if needed
	"current_mode": {
		"address": 42000,
		"type": "uint16"
	},
	// inputs, possible, but we don't use yet.
	"inputs": [
	],
// This is done when the definition is disabled. We ensure the storage does not just continue what has been handed over last
	"resets": [
		{
			// Enabel manual mode, we want to make sure we can send the stop signals properly
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42000&val=21930&type=w"
		},
		{
			// LADEleistung in Watt, auf "0" setzen
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42020&val=0&type=w"
		},
		{
			// ENTladeleistung in Watt, auf "0" setzen
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42021&val=0&type=w"
		},
		{
			// Disable charge/discharge
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42010&val=0&type=w"
		},
		{
			// Disable manual mode
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42000&val=21947&type=w"
		}
	],
// Outputs are used to start modes and tell the storage what to do ...
	"outputs": [
		{
			// Batteriesteuerung aktivieren, nur wenn nicht offen
			"address": "${if bat_mode > 0 && current_mode != 21930`http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42000&val=21930&type=w`}"
		},
		{
			// LADEleistung in Watt, entsprechend Laderegeln, nur im aktiven bat_mode, sonst 0
			"address": "${if bat_mode > 0`http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42020&val=${exists(charge_power_w)? max(0,(charge_power_w-200)/100*100) : 0}&type=w`}"
		},
		{
			// ENTladeleistung Watt, entsprechend Laderegeln, nur im aktiven bat_mode, sonst 0
			// Wir reduzieren ladeleistung immer um 100 Watt und runden ab, um unnötigen Netzbezug zu vermeiden
			"address": "${if bat_mode > 0`http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42021&val=${exists(discharge_power_w)? discharge_power_w : 0}&type=w`}"
		},
		{
			// Write register 42010 based on required status
			"address": "http://localhost/cnf?cmd=modbus_set&addr=${address}&func=6&id=${id}&reg=42010&val=${bat_mode > 0 ? (exists(charge_power_w) && charge_power_w > 0 ? 1 : (exists(discharge_power_w) && discharge_power_w > 0 ? 2 : 0)) : 0}&type=w"
		}
	]
}