Creating switches in Node RED and how to use it?

Hello,

I spent more than a few hours today by trying out the “entity” (type=switch) node. But I somehow don’t get it at all. I also can’t find useful info about it.

I have two custom switches and one custom sensor in my configuration files that I would like two replace with Node-Red created once. I know how to create the Switch and populate it to Home Assistant. But how do I use the created switch? It has one input and two outputs. I tried to inject a few things in the input, but the switch did not change on HA Frontend. I also tried to put two debug nodes two the output and tried to control the switch from HA but I never saw a debug message.

Can please someone explain how to use this node? I need to have the possibility to react to changes (on/off) made in HA and update the state of the switch in the automation. If the user turns the switch on, I need to do something and eventually set the state again to off or on, depending on other stuff. I also need to listen to a few different events and set the state of the switch depending on those.

I can post the yaml code for the switches if you need them, but I just would like to understand how to use this node, so I could create this by myself.

Thanks in advance!

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/custom_integration/switch.html

The next release will add the ability to have the node output on its state change.

Thanks for your response, but I’m still not sure how to do it.

How should I transform this YAML into the Node-Red Code?

- platform: template
    switches:
        # XBOX HARMONY ACTIVITY
        harmony_xbox_one:
          unique_id: 49c74c98-dadc-4a5b-8cf0-bd5135b9faeb
          icon_template: mdi:microsoft-xbox
          friendly_name: Xbox One
          value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Xbox One') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'Xbox One'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'PowerOff'
        
        # BLU-RAY HARMONY ACTIVITY
        harmony_blu_ray:
          unique_id: f4ff8445-ccbb-4155-8ddf-435567785337
          friendly_name: Blu-Ray
          icon_template: mdi:filmstrip
          value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Blu-ray') }}"
          turn_on:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'Blu-ray'
          turn_off:
            service: remote.turn_on
            data:
              entity_id: remote.harmony_hub
              activity: 'PowerOff'

and

sensor:
  - platform: template
    sensors:
      harmony_activity:
        unique_id: b3f483f7-7c04-46bd-ad46-a99e30e0b434
        value_template: >
          {% if is_state("remote.harmony_hub", 'on') %}
            {{ states.remote.harmony_hub.attributes.current_activity }}
          {% else %}
            PowerOff
          {% endif %}

The entity switch node was originally intended to be used as a switch in NR which when received input would output it either to the first or second output based on the switch state in HA. Basically how most people use input_booleans as a conditional.

It was never intended to replace a template switch. This can be made to work though.

This example will only work on the current dev build or next release 0.30.0. Not really tested but should be close to a working example.

[{"id":"95b38784.958838","type":"subflow","name":"Cycle Device","info":"","category":"","in":[{"x":52,"y":128,"wires":[{"id":"67ee25d2.c7aa5c"}]}],"out":[{"x":548,"y":128,"wires":[{"id":"ed283aa6.5bb5b8","port":0},{"id":"5246bc08.8de004","port":0}]}],"env":[{"name":"device","type":"str","value":"","ui":{"type":"select","opts":{"opts":[{"l":{"en-US":"Xbox One"},"v":"Xbox One"},{"l":{"en-US":"Blu-Ray"},"v":"Blu-Ray"}]}}}],"color":"#DDAA99"},{"id":"cf604723.973cc8","type":"switch","z":"95b38784.958838","name":"active","property":"data.attributes.current_activity","propertyType":"msg","rules":[{"t":"eq","v":"device","vt":"env"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":80,"wires":[["ed283aa6.5bb5b8"],["5246bc08.8de004"]]},{"id":"ed283aa6.5bb5b8","type":"change","z":"95b38784.958838","name":"on","rules":[{"t":"set","p":"enable","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":418,"y":80,"wires":[[]]},{"id":"5246bc08.8de004","type":"change","z":"95b38784.958838","name":"off","rules":[{"t":"set","p":"enable","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":418,"y":128,"wires":[[]]},{"id":"67ee25d2.c7aa5c","type":"switch","z":"95b38784.958838","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":162,"y":128,"wires":[["cf604723.973cc8"],["5246bc08.8de004"]]},{"id":"28cac11.6a8a13e","type":"ha-entity","z":"bd7364db.24ff28","name":"Xbox One","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Xbox One"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:microsoft-xbox"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? \"Xbox One\": \"PowerOff\"","outputPayloadType":"jsonata","x":908,"y":464,"wires":[["238cbe09.08b142"],["238cbe09.08b142"]]},{"id":"238cbe09.08b142","type":"api-call-service","z":"bd7364db.24ff28","name":"","server":"","version":1,"debugenabled":true,"service_domain":"remote","service":"turn_on","entityId":"remote.harmony_hub","data":"{\"activity\": payload}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1124,"y":464,"wires":[[]]},{"id":"60890d0d.7028b4","type":"ha-entity","z":"bd7364db.24ff28","name":"Blu-Ray","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Blu-Ray"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:microsoft-xbox"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? \"Blu-Ray\": \"PowerOff\"","outputPayloadType":"jsonata","x":908,"y":512,"wires":[["238cbe09.08b142"],["238cbe09.08b142"]]},{"id":"654e0b1d.60a094","type":"server-state-changed","z":"bd7364db.24ff28","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"remote.harmony_hub","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":438,"y":464,"wires":[["ce3424f3.c3d878","2efcf73.2695008"]]},{"id":"ce3424f3.c3d878","type":"subflow:95b38784.958838","z":"bd7364db.24ff28","name":"","env":[{"name":"device","value":"Xbox One","type":"str"}],"x":726,"y":464,"wires":[["28cac11.6a8a13e"]]},{"id":"2efcf73.2695008","type":"subflow:95b38784.958838","z":"bd7364db.24ff28","name":"","env":[{"name":"device","value":"Blu-Ray","type":"str"}],"x":726,"y":512,"wires":[["60890d0d.7028b4"]]}]
1 Like

Thank you.

I will look forward to the next release and give it then a go.

Thx, again.

Can you give me some guidance on the same issue? I am trying to create a switch in node red that gets its state from a template sensor. When I turn the switch on, it should trigger a script. Off should trigger the same script.

1 Like