I have made this. this might work:
var pGridForecast = msg.payload || 0;
var pBattForecast = msg.pBattForecast || 0;
var inverter1SOC = msg.inverter1_battery_soc || 0;
if (inverter1SOC > 90) {
msg.payload = 40; // Skip updating status
} else if (inverter1SOC < 35 && pGridForecast > 0 && pBattForecast === 0) {
msg.payload = 0; // Update status to 0
} else if (pGridForecast > 0 && pBattForecast === 0) {
msg.payload = 5; // Update status to 5
} else {
msg.payload = 40; // Update status to 40
}
msg.pBattForecast = pBattForecast;
return msg;
noderedflow:
[{"id":"d14f79f6.89f5b8","type":"server-state-changed","z":"b8d78ce2.c7d748","name":"P-grid-forecast","server":"b5e5456a.81935","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.p_grid_forecast","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":210,"y":160,"wires":[["d71a7e46.4e16b"]]},{"id":"b3621522.a38938","type":"server-state-changed","z":"b8d78ce2.c7d748","name":"P-batt-forecast","server":"b5e5456a.81935","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.p_batt_forecast","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":180,"y":280,"wires":[["d71a7e46.4e16b"]]},{"id":"d71a7e46.4e16b","type":"function","z":"b8d78ce2.c7d748","name":"Sjekk betingelser","func":"var pGridForecast = msg.payload || 0;\nvar pBattForecast = msg.pBattForecast || 0;\nvar inverter1SOC = msg.inverter1_battery_soc || 0;\n\nif (inverter1SOC > 90) {\n msg.payload = 40; // Skip updating status\n} else if (inverter1SOC < 35 && pGridForecast > 0 && pBattForecast === 0) {\n msg.payload = 0; // Update status to 0\n} else if (pGridForecast > 0 && pBattForecast === 0) {\n msg.payload = 5; // Update status to 5\n} else {\n msg.payload = 40; // Update status to 40\n}\n\nmsg.pBattForecast = pBattForecast;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":220,"wires":[["72c3abf8.3efb7c"]]},{"id":"72c3abf8.3efb7c","type":"api-call-service","z":"b8d78ce2.c7d748","name":"Oppdater status","server":"b5e5456a.81935","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_text.emhass_deye_charge_amps"],"data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":780,"y":220,"wires":[[]]},{"id":"3cbf2a09510eb66a","type":"server-state-changed","z":"b8d78ce2.c7d748","name":"SOC","server":"b5e5456a.81935","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.inverter1_battery_soc","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":190,"y":100,"wires":[["d71a7e46.4e16b"]]},{"id":"b5e5456a.81935","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]