Need Help with Node-RED Integration: S7 Status and HomeAssistant Switch

Hi everyone,

I hope you’re doing well. I’m currently working on a project involving Node-RED, HomeAssistant, and an S7 PLC, and I’m facing a challenge that I could use some assistance with.

Here’s the situation:

I’ve set up a switch using the switch node in Node-RED, utilizing HomeAssistant entities. This switch controls an S7 output via the contrib S7 node. Simultaneously, I have an S7 input reflecting the status of the S7 with true or false. The switch operates a light, which is also independently controlled outside of HomeAssistant.

Now, my goal is to synchronize the switch’s status with the S7 input, so if the light is turned on, for instance, via a touch panel, HomeAssistant should reflect the change in status.

I’ve attempted to link the S7 input to a call service node using “update entity,” but unfortunately, it doesn’t seem to be functioning as expected.

Has anyone encountered a similar scenario or have suggestions on how I could achieve this integration successfully? Your insights and guidance would be greatly appreciated.

Thank you in advance for your help!

Here is my Flow:

[{"id":"3c8bc872742e839d","type":"ha-switch","z":"9ebc301195a1b88f","name":"test","version":0,"debugenabled":false,"inputs":1,"outputs":2,"entityConfig":"01f09b557f59bd97","enableInput":true,"outputOnStateChange":true,"outputProperties":[{"property":"outputType","propertyType":"msg","value":"state change","valueType":"str"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":430,"y":160,"wires":[["e1b43ee7518badc6"],["e1b43ee7518badc6"]]},{"id":"e1b43ee7518badc6","type":"s7 out","z":"9ebc301195a1b88f","endpoint":"4d44a1d23acba674","variable":"Licht","name":"S7 Out (Lichtsteuerung)","x":710,"y":160,"wires":[]},{"id":"cc10b402c1b46390","type":"debug","z":"9ebc301195a1b88f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":440,"y":560,"wires":[]},{"id":"a14618e695ca9658","type":"s7 in","z":"9ebc301195a1b88f","endpoint":"4d44a1d23acba674","mode":"single","variable":"Licht","diff":true,"name":"","x":150,"y":620,"wires":[["cc10b402c1b46390","f4980283f5985386","3c8bc872742e839d"]]},{"id":"a15ad4c4fe3965bb","type":"debug","z":"9ebc301195a1b88f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":440,"y":380,"wires":[]},{"id":"f4980283f5985386","type":"api-call-service","z":"9ebc301195a1b88f","name":"","server":"5846ae4d.395fd","version":5,"debugenabled":false,"domain":"homeassistant","service":"update_entity","areaId":[],"deviceId":[],"entityId":["switch.nodered_01f09b557f59bd97"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":720,"y":700,"wires":[["a15ad4c4fe3965bb"]]},{"id":"01f09b557f59bd97","type":"ha-entity-config","server":"5846ae4d.395fd","deviceConfig":"","name":"Test","version":"6","entityType":"switch","haConfig":[{"property":"name","value":""},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""}],"resend":false,"debugEnabled":false},{"id":"4d44a1d23acba674","type":"s7 endpoint","transport":"iso-on-tcp","address":"192.168.1.150","port":"102","rack":"0","slot":"2","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapter":"","busaddr":"2","cycletime":"1000","timeout":"5000","name":"","vartable":[{"addr":"DB1,X0.0","name":"Licht"},{"addr":"DB6,X0.0","name":"Licht An"},{"addr":"DB6,X0.1","name":"Licht aus"},{"addr":"DB6,X0.2","name":"Status"}]},{"id":"5846ae4d.395fd","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]

I don’t have those nodes installed but I’ll take a guess at whats happening. From the side panel

msg.enable
Set to true to turn on the switch and false to turn it off.

So if the message from the s7 needs to be a boolean set to true or false for it to work. It’s probably on and off. You can use a change node to manipulate the message.

[{"id":"d68f74460b97d10d","type":"change","z":"60f2d2277843c698","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"bool"},{"t":"set","p":"enable","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":2520,"wires":[["ba136cc2e5caf850"]]},{"id":"8b09f97d4bf24164","type":"inject","z":"60f2d2277843c698","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":570,"y":2480,"wires":[["d68f74460b97d10d"]]},{"id":"48bc0b576a75b553","type":"inject","z":"60f2d2277843c698","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":570,"y":2560,"wires":[["d68f74460b97d10d"]]},{"id":"ba136cc2e5caf850","type":"debug","z":"60f2d2277843c698","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1040,"y":2520,"wires":[]}]

Changing the test switch should update in HA with out having to update the entity in a call service.

1 Like

@Mikefila
Just wanted to say a big thank you for your solution and the quick response. I tried it out, and it worked like a charm right away. I really appreciate your help!

Best regards,

Timo