Enable/Disable automation flow for a room in Node-Red

Hi community!

I’m very new to Home Assistant, so my question could be very simple/dumb.

I created a temperature sensor in my basement and smart switch that controls heater. Everything works fine. Recently I discovered a node red plugin and created “logic” that enable/disable heater’s switch based on temerature sensor and current time. The flow also works fine.

Now I want to create a “logical switch” that enable disable the flow. For instance, if I’m on vacation I don’t want the whole automation to run. How should I do it?

I read about input boolean and specified it in configuration.yml:

input_boolean:
  basement_automation_value:
    name: "Basement automation"
    icon: mdi:switch
    initial: on

However, I have 2 problems:

  1. Even though I can add the boolean to home screen I can’t toggle it
  2. I can’t add logic in Node-Red based on the boolean

What am I doing wrong?

Your help is very appreciated

Hi there…Please share the flow and also the code that you have used to deploy the button in home screen. Without it, we might have to start from scratch. You can share the code in nodered by selection the flow and pressing CTRL+E.

The flow doesn’t have the boolean input yet. That’s what I’m trying to add. Currently, it looks like

[
    {
        "id": "bf2970e6.9084c",
        "type": "tab",
        "label": "Basement Automation",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f4afaeb3.4b894",
        "type": "poll-state",
        "z": "bf2970e6.9084c",
        "name": "Too Cold?",
        "server": "50ca6ff0.8283b",
        "version": 1,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "updateinterval": "60",
        "updateIntervalUnits": "seconds",
        "outputinitially": true,
        "outputonchanged": true,
        "entity_id": "sensor.basement_temperature",
        "state_type": "str",
        "halt_if": "66",
        "halt_if_type": "num",
        "halt_if_compare": "lt",
        "outputs": 2,
        "x": 140,
        "y": 200,
        "wires": [
            [
                "841123da.26907"
            ],
            []
        ]
    },
    {
        "id": "54938108.71ed1",
        "type": "api-call-service",
        "z": "bf2970e6.9084c",
        "name": "Turn heater ON",
        "server": "50ca6ff0.8283b",
        "version": 1,
        "debugenabled": true,
        "service_domain": "light",
        "service": "turn_on",
        "entityId": "light.power_switch",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 660,
        "y": 160,
        "wires": [
            []
        ]
    },
    {
        "id": "1c048a20.00de86",
        "type": "poll-state",
        "z": "bf2970e6.9084c",
        "name": "Too Hot?",
        "server": "50ca6ff0.8283b",
        "version": 1,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "updateinterval": "60",
        "updateIntervalUnits": "seconds",
        "outputinitially": true,
        "outputonchanged": true,
        "entity_id": "sensor.basement_temperature",
        "state_type": "str",
        "halt_if": "71",
        "halt_if_type": "num",
        "halt_if_compare": "gt",
        "outputs": 2,
        "x": 140,
        "y": 320,
        "wires": [
            [
                "d5c7ef42.9ee19"
            ],
            []
        ]
    },
    {
        "id": "d5c7ef42.9ee19",
        "type": "api-call-service",
        "z": "bf2970e6.9084c",
        "name": "Turn heater OFF",
        "server": "50ca6ff0.8283b",
        "version": 1,
        "debugenabled": true,
        "service_domain": "light",
        "service": "turn_off",
        "entityId": "light.power_switch",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 660,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "841123da.26907",
        "type": "time-range-switch",
        "z": "bf2970e6.9084c",
        "name": "Day",
        "lat": "",
        "lon": "",
        "startTime": "07:00",
        "endTime": "21:00",
        "startOffset": 0,
        "endOffset": 0,
        "x": 370,
        "y": 200,
        "wires": [
            [
                "54938108.71ed1"
            ],
            [
                "d5c7ef42.9ee19"
            ]
        ]
    },
    {
        "id": "50ca6ff0.8283b",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

Code to deploy the boolean to home screen:

type: entity
entity: switch.basement_automation_switch
attribute: friendly_name

Configuration.yaml:

input_boolean:
  basement_automation_value:
    name: "Basement automation"
    icon: mdi:switch
    initial: on

switch:
  - platform: template
    switches:
      basement_automation_switch:
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.basement_automation
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.basement_automation

Couple things jump out at me:

  1. Why are you polling? If you use an events:state or trigger:state node then you can have the automation trigger as soon as sensor.basement_temperature crosses 66 or 71. Polling seems unnecessary unless I’m missing something?
  2. You don’t really need to make input_boolean helpers with node red. Any of the starter nodes like events:state and trigger:state have a checkbox for “expose to HA”. If you check the box it creates a switch entity. When that switch is on the node is enabled, when it is off the node is disabled. This allows you to turn a flow on/off on the fly
  3. You can also dynamically disable parts of flows if you want by including an entity node of type switch in the middle of a flow. This also creates a switch in HA that when set to on allows messages to pass through and when set to off stops all messages from continuing

I tweaked your flow a bit to use a trigger:state that can be toggled with a switch. Give it a shot, let me know if that works for you or is a bit different then what you were going for

[{"id":"bf2970e6.9084c","type":"tab","label":"Basement Automation","disabled":false,"info":""},{"id":"54938108.71ed1","type":"api-call-service","z":"bf2970e6.9084c","name":"Turn heater ON","server":"50ca6ff0.8283b","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.power_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":660,"y":160,"wires":[[]]},{"id":"d5c7ef42.9ee19","type":"api-call-service","z":"bf2970e6.9084c","name":"Turn heater OFF","server":"50ca6ff0.8283b","version":1,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.power_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":660,"y":320,"wires":[[]]},{"id":"841123da.26907","type":"time-range-switch","z":"bf2970e6.9084c","name":"Day","lat":"","lon":"","startTime":"07:00","endTime":"21:00","startOffset":0,"endOffset":0,"x":370,"y":220,"wires":[["54938108.71ed1"],["d5c7ef42.9ee19"]]},{"id":"b37411db.e8e248","type":"trigger-state","z":"bf2970e6.9084c","name":"Basement temp","server":"cc03735a.94933","exposeToHomeAssistant":true,"haConfig":[{"property":"name","value":"Basement automation"},{"property":"icon","value":""}],"entityid":"sensor.basement_temperature","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is_not","comparatorValueDatatype":"prevEntity","comparatorValue":"state","propertyValue":"new_state.state"}],"outputs":4,"customoutputs":[{"messageType":"default","messageValue":"","messageValueType":"json","comparatorPropertyType":"current_state","comparatorType":"<","comparatorValue":"66","comparatorValueDataType":"num","comparatorPropertyValue":"new_state.state"},{"messageType":"default","messageValue":"","messageValueType":"json","comparatorPropertyType":"current_state","comparatorType":">","comparatorValue":"71","comparatorValueDataType":"num","comparatorPropertyValue":"new_state.state"}],"outputinitially":false,"state_type":"num","x":180,"y":240,"wires":[[],[],["841123da.26907"],["d5c7ef42.9ee19"]]},{"id":"50ca6ff0.8283b","type":"server","name":"Home Assistant","addon":true},{"id":"cc03735a.94933","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

There are multiple ways to solve this. I will explain how this can be done without adding any new custom integrations to your HA. We will work with the input Boolean basement_automation_value. You have to add a current state node to your flow to check the state of basement_automation_value. This will check the state of this input_boolean and only pass the payload if this is on. The flow will look like this.

[{"id":"f4afaeb3.4b894","type":"poll-state","z":"bf2970e6.9084c","name":"Too Cold?","server":"50ca6ff0.8283b","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalUnits":"seconds","outputinitially":true,"outputonchanged":true,"entity_id":"sensor.basement_temperature","state_type":"str","halt_if":"66","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"x":140,"y":200,"wires":[["841123da.26907"],[]]},{"id":"54938108.71ed1","type":"api-call-service","z":"bf2970e6.9084c","name":"Turn heater ON","server":"50ca6ff0.8283b","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.power_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":180,"wires":[[]]},{"id":"1c048a20.00de86","type":"poll-state","z":"bf2970e6.9084c","name":"Too Hot?","server":"50ca6ff0.8283b","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalUnits":"seconds","outputinitially":true,"outputonchanged":true,"entity_id":"sensor.basement_temperature","state_type":"str","halt_if":"71","halt_if_type":"num","halt_if_compare":"gt","outputs":2,"x":140,"y":320,"wires":[["5403a207.a0b6bc"],[]]},{"id":"d5c7ef42.9ee19","type":"api-call-service","z":"bf2970e6.9084c","name":"Turn heater OFF","server":"50ca6ff0.8283b","version":1,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.power_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":830,"y":320,"wires":[[]]},{"id":"841123da.26907","type":"time-range-switch","z":"bf2970e6.9084c","name":"Day","lat":"","lon":"","startTime":"07:00","endTime":"21:00","startOffset":0,"endOffset":0,"x":370,"y":200,"wires":[["4c307ab3.942a04"],["5403a207.a0b6bc"]]},{"id":"4c307ab3.942a04","type":"api-current-state","z":"bf2970e6.9084c","name":"basement_automation_value","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.basement_automation_value","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":580,"y":180,"wires":[["54938108.71ed1"],[]]},{"id":"5403a207.a0b6bc","type":"api-current-state","z":"bf2970e6.9084c","name":"basement_automation_value","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.basement_automation_value","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":580,"y":320,"wires":[["d5c7ef42.9ee19"],[]]},{"id":"50ca6ff0.8283b","type":"server","name":"Home Assistant","addon":true},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Please check if the input boolean name is correct and then deploy.

Now with the home screen. I have seen that you have used entity card, the purpose of which is to show the state of the entity. If you want to use the basement_automation_value boolean without any issues you should use either a button card or an entities card. Try it and let us now if there are any more issues.

You absolutely wont need the other template switch as every operation can be done with this boolean.

I also can explain an advanced solution but this needs you to install HACS and node red custom component. In this method you can directly by using expose to HA

Hi Mike,

Thanks a lot for your recommendations/comments.

  1. Indeed, I don’t need the polling, it was the first solution that worked for me.

  2. I deleted my flow and imported your solution. While opening node “Basement temp” I get the errors:
    “You probably haven’t deployed since adding a server. Do that for autocomplete to work…”
    After deployment, these errors disappear, but I don’t have “Expose to Home Assistant” check box:


    So I’m coming back to square one when I can’t disable the automation from the home screen

  3. I tried to add an entity switch but it also doesn’t have “expose to HA”

Hi sheminasalam,

I tried your solution, but it can’t find “input_boolean.basement_automation_value”, autocomplete also can’t find it in the list of entities. Note that I removed template switch as you recommended.

For the home screen, I used “entities” card and it works as I would expect :slight_smile:
image

type: entities
entities:
  - entity: input_boolean.basement_automation_value

By input_boolean.basement_automation_value, I was referring to the above inout_boolean. I dont know what the exact entity_id of this input boolean, that why i said

This message appears any time you import flows from someone else with HA nodes. You see at the top of the node where it says “Server: Home Assistant” in a dropdown? That’s actually referencing a config node and each config node gets a generated GUID behind the scenes. When you imported my code it was referencing the GUID for my config node but yours has a different ID. All you have to do to fix it is click the dropdown and click “Home Assistant” then press OK and the error will go away.

Entity nodes don’t have this option because it’s assumed. When you add an entity node to your flow what you are really doing is exposing an entity in HA that is connected with that node, that’s the entire point of that node. So you don’t need a checkbox to expose to HA since that would be redundant.

If you add an entity node of type switch to the screen and click deploy then you should immediately see a switch enitity appear in HA. Search for the name you picked in developer tools and you should be able to find it. Toggle it on and off and you’ll see that the node in your flow switches to DISABLED when its set to off and re-enables when flipped back on.

The “expose to HA” option I was referring to is on nodes of type “entity: state”, “trigger: state”, “events: all” and “poll: state”. These are the nodes I refer to as the “starter” nodes because they listen on HA’s event bus and create new messages when the messages meet whatever criteria they are looking for. These nodes also have an “expose to HA” option at the bottom, like this:


When you check the box those options at the bottom appear asking you to provide a name and icon for the entity. When this node is deployed like this then it will create a switch type entity with the name you specified that will disable/enable this node dynamically. This allows you to turn entire flows on and off either manually or via automation without requiring a re-deploy

1 Like

Thanks sheminasalam.

Your solution is working! I just don’t know why nodered doesn’t provide autocomplete for input_boolean.basement_automation_value. Probably that’s why I was confused:
image

Sometimes you have to restart node red to get new entities in the auto population tab. Just try restarting nodered.

You are right, nodered’s restart did the magic

1 Like

Hi Mike,

Sorry but I still don’t see an option “expose to HA” in “trigger: state” node. I took your solution and here is how it looks in nodered UI:


Note that highlighted property ‘exposeToHomeAssistant’ of the node is set to false and I can’t change it (at least I don’t know how to )
Regarding to an entity node, I’ve added the node (not connected to anything) deployed the flow but can’t see it in HA.
Could it be due to a warning that I see while editing the node: " Attention: This node requires Node-RED custom integration to be installed in Home Assistant for it to function."?

Ah, yes. I’m sorry I installed that so long ago I forgot all about that.

Yes the ability to create entities in HA linked to nodes is only available if you install this custom component from HACS. It’s made by the same person that makes the HA nodes in Node RED and provides a lot of additional useful functionality including the ability to create switches for nodes, use the entity node and use the webhooks node.

I would recommend installing it but if you don’t want to then yes you will need to use helpers like input_booleans here instead.

Thank you guys, I’m using HA for 1 year with several flows but had never understood the purpose of “Expose do Home Assistant” function.