Can an Input Boolean be replaced by a function?

The more I learn about node red the more I realise how powerful it is. And the more I can do without going back and forth between nr and hassio I feel the better (and cleaner) it all is.

I was looking at a post about motion overriding a light switch.

https://community.home-assistant.io/t/light-with-motion-override-with-switch/62575

But for this to work there are a collection of input Boolean switches that just record a state for a trigger. I was thinking, could this be done via a function.

I believe I have the framework, just not the coding skills yet.

Could it be possible rather than sending a call service to HA to switch an input Boolean switch to be a flow wide variable that can be recalled and acted upon as in the link above?

Basic motion sensor turns light on and off on the right with a trigger node. The left watches for a state change of the light and checks if the context.user_id is null which normally means that the device changed state without Home Assistant acting on it. Which then disables/enables, depending on the state of the light, the trigger-state node and resets the trigger node if it has an active timer.

[{"id":"77cc1128.5f277","type":"trigger-state","z":"2c4951db.68769e","name":"Motion?","server":"ef067c6f.620e6","entityid":"sensor.motion","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"ea06aae1s78","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":524,"y":272,"wires":[["86e5e29b.daed7","8c02e86a.e060c8"],[]]},{"id":"87ce41b9.0e8af","type":"trigger-state","z":"2c4951db.68769e","name":"light turned on","server":"8d00ebbc.99a928","entityid":"light.light","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":4,"customoutputs":[{"outputId":"9e4bvhks1xf","messageType":"custom","messageValue":"{\"reset\": true, \"payload\": \"disable\"}","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"on"},{"outputId":"666vvs6uk38","messageType":"custom","messageValue":"{\"reset\": true, \"payload\": \"enable\"}","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"off"}],"outputinitially":false,"state_type":"str","x":176,"y":240,"wires":[[],[],["bdfa623.03489a"],["77cc1128.5f277","8c02e86a.e060c8"]]},{"id":"8c02e86a.e060c8","type":"trigger","z":"2c4951db.68769e","op1":"","op2":"1","op1type":"nul","op2type":"str","duration":"5","extend":true,"units":"min","reset":"","bytopic":"all","name":"","x":710,"y":224,"wires":[["5a7b22f5.ba884c"]]},{"id":"5a7b22f5.ba884c","type":"api-call-service","z":"2c4951db.68769e","name":"Light Off","server":"ef067c6f.620e6","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.light\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":876,"y":224,"wires":[[]]},{"id":"86e5e29b.daed7","type":"api-call-service","z":"2c4951db.68769e","name":"Light On","server":"ef067c6f.620e6","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.light\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":700,"y":272,"wires":[[]]},{"id":"bdfa623.03489a","type":"switch","z":"2c4951db.68769e","name":"user_id null?","property":"data.context.user_id","propertyType":"msg","rules":[{"t":"null"}],"checkall":"true","repair":false,"outputs":1,"x":358,"y":224,"wires":[["77cc1128.5f277","8c02e86a.e060c8"]]},{"id":"ef067c6f.620e6","type":"server","z":"","name":"hass.io","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]
1 Like

Kermit

I can see the brilliance in this. I have been trying to understand state triggers better. This is just so clever.

Unfortunately, it did not work. I think the user_id null is not working as intended. It took me a while to try and have the flow in a state where it would work, but as soon as there is motion it disables the motion sensor.

Just doing some isolation testes.

First and second message data is from a manual switch.

third and forth is from switching it on from Homeassistant.

Not have the userid component.

This is with a Current state node and a manual switch

And last test. Using Home Assistant to trigger the light.

Hass the user_id as Null

Yea, I see the error I made with the user_id is null switch by sending a custom message it’s not passing on the other message data. You can fix this by changing the first custom output to the example below:

Then you can add a change node after the switch with the reset command

So it ends up looking something like

As far as user_id being null for everything. Do you run home assistant with a trusted network or something where you don’t log into the web ui?

I got it close to 100%. The user ID is now correctly reporting as Null if I manually trigger the light… Well not always.

After the motion trigger event has switched my light off the next (only) manual on will send a user_id, a manual off then on will set it back to null, however.

Off message sent

The next trigger is manual from the switch. It shows a User ID

I can turn the light manually off then back on and its sends user ID as Null.

I wonder if I can sell that to my wife as a user feature?

I just tested it with another light. I assume I have done something wrong, but the manual switch often sends a user ID. And sometimes it does not.

Any suggestions on my error?

    [{"id":"6edc9969.5eadf8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ca51857b.d33f88","type":"inject","z":"6edc9969.5eadf8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":930,"y":220,"wires":[["8b2eb038.dde2e"]]},{"id":"8b2eb038.dde2e","type":"api-call-service","z":"6edc9969.5eadf8","name":"","server":"ea5ed475.5c5908","service_domain":"light","service":"toggle","data":"{\"entity_id\":\"light.office_main_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":1100,"y":220,"wires":[[]]},{"id":"5b4d3435.08f69c","type":"trigger-state","z":"6edc9969.5eadf8","name":"Office light turned on","server":"ea5ed475.5c5908","entityid":"light.office_main_lights","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":4,"customoutputs":[{"outputId":"hp43dzgo8xl","messageType":"default","messageValue":"","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"on"},{"outputId":"t2ibsjrm5jf","messageType":"custom","messageValue":"{\"reset\": true, \"payload\": \"enable\"}","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"off"}],"outputinitially":false,"state_type":"str","x":120,"y":140,"wires":[[],[],["a42691be.fefd5","ad939540.2a9728"],["71a5a944.1ae528","e1b18931.7153e8"]]},{"id":"71a5a944.1ae528","type":"trigger-state","z":"6edc9969.5eadf8","name":"Motion?","server":"ea5ed475.5c5908","entityid":"binary_sensor.template_last_office_motion","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"ea06aae1s78","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":360,"y":260,"wires":[["8bd9e45.a16f118","e1b18931.7153e8","9a28a705.21f248"],[]]},{"id":"8bd9e45.a16f118","type":"debug","z":"6edc9969.5eadf8","name":"Light On","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":680,"y":320,"wires":[]},{"id":"e1b18931.7153e8","type":"trigger","z":"6edc9969.5eadf8","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"10","extend":true,"units":"min","reset":"","bytopic":"all","name":"","x":710,"y":120,"wires":[["9ac6b4e7.fae798","97fcf15e.c201c"]]},{"id":"9ac6b4e7.fae798","type":"debug","z":"6edc9969.5eadf8","name":"Light Off","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":860,"y":40,"wires":[]},{"id":"a42691be.fefd5","type":"switch","z":"6edc9969.5eadf8","name":"user_id null?","property":"data.event.new_state.context.user_id","propertyType":"msg","rules":[{"t":"null"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":120,"wires":[["4decaee6.cb3d1","85f0f0d8.ef85f","e76099d5.8231e8"]]},{"id":"4decaee6.cb3d1","type":"change","z":"6edc9969.5eadf8","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":120,"wires":[["e1b18931.7153e8"]]},{"id":"85f0f0d8.ef85f","type":"change","z":"6edc9969.5eadf8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"disable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":200,"wires":[["71a5a944.1ae528","4e6cae9.f04d55"]]},{"id":"9a28a705.21f248","type":"api-call-service","z":"6edc9969.5eadf8","name":"Light On","server":"ea5ed475.5c5908","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.office_main_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":660,"y":260,"wires":[[]]},{"id":"97fcf15e.c201c","type":"api-call-service","z":"6edc9969.5eadf8","name":"Light Off","server":"ea5ed475.5c5908","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.office_main_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":900,"y":120,"wires":[[]]},{"id":"e76099d5.8231e8","type":"debug","z":"6edc9969.5eadf8","name":"Null","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":530,"y":40,"wires":[]},{"id":"4e6cae9.f04d55","type":"debug","z":"6edc9969.5eadf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":610,"y":200,"wires":[]},{"id":"ad939540.2a9728","type":"debug","z":"6edc9969.5eadf8","name":" Raw","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":330,"y":40,"wires":[]},{"id":"5c168e6d.d6b6f","type":"inject","z":"6edc9969.5eadf8","name":"","topic":"","payload":"enable","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":240,"wires":[["71a5a944.1ae528"]]},{"id":"6e69171c.7002a8","type":"inject","z":"6edc9969.5eadf8","name":"","topic":"","payload":"disable","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":280,"wires":[["71a5a944.1ae528"]]},{"id":"216c99e.e7ed566","type":"trigger-state","z":"6edc9969.5eadf8","name":"light turned on","server":"ea5ed475.5c5908","entityid":"light.ground_floor_nook_lights","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":4,"customoutputs":[{"outputId":"hp43dzgo8xl","messageType":"default","messageValue":"","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"on"},{"outputId":"t2ibsjrm5jf","messageType":"custom","messageValue":"{\"reset\": true, \"payload\": \"enable\"}","comparatorPropertyType":"current_state","comparatorPropertyValue":"new_state.state","comparatorType":"is","comparatorValue":"off"}],"outputinitially":false,"state_type":"str","x":160,"y":640,"wires":[[],[],["c434a74e.729818","60a00920.702238"],["fe64604a.f37c6","d752ce7b.523a3"]]},{"id":"fe64604a.f37c6","type":"trigger-state","z":"6edc9969.5eadf8","name":"Motion?","server":"ea5ed475.5c5908","entityid":"binary_sensor.living_room_motion","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"ea06aae1s78","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":400,"y":760,"wires":[["14a21dd2.8cf1e2","d752ce7b.523a3","cae2242f.aa49d8"],[]]},{"id":"14a21dd2.8cf1e2","type":"debug","z":"6edc9969.5eadf8","name":"Light On","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":720,"y":820,"wires":[]},{"id":"d752ce7b.523a3","type":"trigger","z":"6edc9969.5eadf8","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"10","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":750,"y":620,"wires":[["b47d3eef.669bd","6fc6629a.40ac0c"]]},{"id":"b47d3eef.669bd","type":"debug","z":"6edc9969.5eadf8","name":"Light Off","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":900,"y":540,"wires":[]},{"id":"c434a74e.729818","type":"switch","z":"6edc9969.5eadf8","name":"user_id null?","property":"data.event.new_state.context.user_id","propertyType":"msg","rules":[{"t":"null"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":620,"wires":[["34a4b28a.a581be","e9fc6e32.ee0af","e917f898.7ec148"]]},{"id":"34a4b28a.a581be","type":"change","z":"6edc9969.5eadf8","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":620,"wires":[["d752ce7b.523a3"]]},{"id":"e9fc6e32.ee0af","type":"change","z":"6edc9969.5eadf8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"disable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":700,"wires":[["fe64604a.f37c6","65fe702.161cb9"]]},{"id":"cae2242f.aa49d8","type":"api-call-service","z":"6edc9969.5eadf8","name":"Light On","server":"ea5ed475.5c5908","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.ground_floor_nook_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":700,"y":760,"wires":[[]]},{"id":"6fc6629a.40ac0c","type":"api-call-service","z":"6edc9969.5eadf8","name":"Light Off","server":"ea5ed475.5c5908","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.ground_floor_nook_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":940,"y":620,"wires":[[]]},{"id":"e917f898.7ec148","type":"debug","z":"6edc9969.5eadf8","name":"Null","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":570,"y":540,"wires":[]},{"id":"65fe702.161cb9","type":"debug","z":"6edc9969.5eadf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":650,"y":700,"wires":[]},{"id":"60a00920.702238","type":"debug","z":"6edc9969.5eadf8","name":" Raw","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":370,"y":540,"wires":[]},{"id":"d272dd69.478d4","type":"inject","z":"6edc9969.5eadf8","name":"","topic":"","payload":"enable","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":740,"wires":[["fe64604a.f37c6"]]},{"id":"a5b3d72.0905728","type":"inject","z":"6edc9969.5eadf8","name":"","topic":"","payload":"disable","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":780,"wires":[["fe64604a.f37c6"]]},{"id":"ea5ed475.5c5908","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

This is another way I tackled the issue

I changed the TASMOTA switchmode to 2 and gave a custom topic. I have node red watching the topic that will then turn the light on or off via MQTT. But I am able to disable the automation with this method.

This is version one. I need to get the physical and virtual switch into sync. But its a start.

[{"id":"c890543e.860308","type":"api-call-service","z":"6e57f89c.1f9338","name":"","server":"ea5ed475.5c5908","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.office_main_lights\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":1090,"y":360,"wires":[[]]},{"id":"ea5ed475.5c5908","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

@Kermit It has been some time but I may have stumbled onto a breakthrough. My issue above was HA not recognizing the Null user ID for turning on/off a physical switch.

I was converting my Sonoff switches to ESPhome and the code I was using used both a Physical and a HA toggle switch.

I was wondering if you could guide me in the above flow to enable the motion trigger is disabled if the physical switch has been used. For your refrence, I have included the yaml for ESPhome that shows the two switches.

Thanks

 output:
  • platform: gpio
    pin: GPIO12
    id: main_relay
    light:
  • platform: binary
    name: “Office Light”
    id: office_main_light
    output: main_relay

binary_sensor:

  • platform: gpio
    pin:
    number: GPIO14
    mode: INPUT_PULLUP
    inverted: True
    name: “Office Physical Switch Sensor”
    on_press:
    then:
    - light.toggle: office_main_light
    on_release:
    then:
    - light.toggle: office_main_light

@Kermit - I feel like this was working for me just a few minutes ago but this morning its not… every time the motion automation triggers the flow I end up w/ the motion sensor being disabled… i tried to impliment this update but I’m having trouble still… Any change you can post a working example from you flows?