Stop input_boolean from triggering on system restart

I have an input_boolean.summer_toggle defined to switch my automations from summer and winter mode. All of my climate automations check this to decide what to do.

I have automations that trigger when a window in a room is opened then HA turns the heat off in there and if the window is closed it turns the heat in that room back on.

I also have an automation (Toggle Summer) that triggers directly on that input_boolean.summer_toggle which will turn heating in the entire house on or off depending on the state.

My problem is if a window is open, so the heating in that room is off, then the HA system is reset (like from today’s 2021.4.4 update) then this Toggle Summer automation gets triggered and turns the heat in that room back on while the window is open. Now I could make the Summer Toggle automation check each room with a choose action and only if all windows are closed in that room it will turn the heat on but that increases the complexity by 10x and I’m not 100% confident that as soon as the system comes back the window state will be accurate.

All I really want is that this automation only runs when I manually flip the switch and no other way. Is there a condition I can add to check this?

Not sure how you defined this trigger, but if you haven’t tried this yet, you could explicitly define a state transition, for example:

from: on
to: off
1 Like

You could add a condition that system uptime has to be (say) greater than (or equal to) 1 minute
I use : -

    condition:
      - condition: template
        value_template: "{{ states('sensor.s_ha_uptime') != '00:00' }}"

from my sensor : -

  - platform: template
    sensors:
      s_ha_uptime:
        friendly_name: HA Current Uptime
        value_template: >
          {% set secs = as_timestamp(states.sensor.time.last_updated) - as_timestamp(states('sensor.uptime')) %}
          {% set days = (secs / (24 * 60 * 60)) | int %}
          {% set plural = ' days ' if days > 1 else ' day ' %}
          {% set hrmn = secs | timestamp_custom('%H:%M', false) %}
          {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

but you could just as easily create a binary sensor and use that for multiple automations

Edit: as Marius says below, for your use case this should be VERY rare. I use this on my boiler switch to prevent changes just after a restart, to ensure my sensors are up to date before taking any actions based on those sensor values

1 Like

using many input_booleans my self to trigger automations, I do not see this at all. Could be you have initial: set on these booleans, and that state is changed during startup?

The whole benefit of the booleans is they survive restarts, preventing this behavior?

Unless I misunderstood you first sentence:

by switch, do you mean trigger, or turn-on/off the automation?

1 Like

and for this specific need, couldn’t you simply use:

{{((as_timestamp(now())- as_timestamp(states('sensor.uptime'))))|int > 60}}

or

{{((as_timestamp(now())-as_timestamp(states('sensor.uptime')))/60)|int > 1}}

to be sure it is past the minute after restart?

I mean, I love the template sensors, have it myself in a nice card, but it is somewhat overkill to use here insn’t it.

If you’d like to get fancy, you could create a binary sensor for that and use that in the condition

or, as I have done somewhere else, create the event delayed_startup (in my case after 35 secs), and trigger automations on that event:

  - alias: Run at startup
    id: Run at startup
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: script.notify_startup
      - service: python_script.family_home
      - delay:
          seconds: >
            {{states('input_number.ha_delayed_startup')|int}}
      - event: delayed_homeassistant_start

  - alias: Run at delayed startup
    id: Run at delayed startup
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:
      - service: script.notify_delayed_startup
      - service: input_boolean.turn_off
        entity_id: input_boolean.just_started
      - service: script.run_after_delayed_startup

note the input_boolean.just_started …:wink: which could be a method nr 3

this one does have the initial: 'on' but that is intended:

input_boolean:

  just_started:
    name: Just started
    initial: 'on'

I have the sensor set up any way and the ONLY time it is 00:00 is in the first 60 seconds following the restart so this was just minimum effort. I’m just a lazy bastard !
:rofl:

1 Like

I think the issue is I need to add from and to. To make it more simple I have no from/to and then use a template of the action to set on or off in the climate. I probably need to add both possibilities.

if anything, you could post your automation and boolean here so we can see what’s happening?

This is what I had

- id: '1618160375670'
  alias: Summer Toggle
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.summer_mode
    for: 00:00:30
  condition: []
  action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: >
        {% if trigger.to_state.state == "on" %}
          off
        {% else %}
          heat
        {% endif %}
    target:
      area_id:
      - bf5a7a66ef8d4932952e7f04c19345d9
      - b01628126e104aeaaf75f58b825dd418
      - 7d8912e9dfae3c7ef53c69144ec4473c
      - 02e2deba0e32794ace31f3b722c7bc15
      - 4492563b0a7600466b3c47ff375a79c7
      - 573832d94ef36bbbac23e1147c5b976b
      - 5dc378600abcc3629a3181a1b7ec96f3
      - c1bcb08d993829bca4a249a696853ce7
      - 1c3a338d355449f5ac3ffd8f274d4d55
      - 0b30d034be014563ab8783219a8064b6
  mode: single

This is what I’m changing it to

- id: '1618160375670'
  alias: Summer Toggle
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.summer_mode
    from: "off"
    to: "on"
    for: 00:00:30
  - platform: state
    entity_id: input_boolean.summer_mode
    from: "on"
    to: "off"
    for: 00:00:30
  condition: []
  action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: >
        {% if trigger.to_state.state == "on" %}
          off
        {% else %}
          heat
        {% endif %}
    target:
      area_id:
      - bf5a7a66ef8d4932952e7f04c19345d9
      - b01628126e104aeaaf75f58b825dd418
      - 7d8912e9dfae3c7ef53c69144ec4473c
      - 02e2deba0e32794ace31f3b722c7bc15
      - 4492563b0a7600466b3c47ff375a79c7
      - 573832d94ef36bbbac23e1147c5b976b
      - 5dc378600abcc3629a3181a1b7ec96f3
      - c1bcb08d993829bca4a249a696853ce7
      - 1c3a338d355449f5ac3ffd8f274d4d55
      - 0b30d034be014563ab8783219a8064b6
  mode: single

This triggers when the input_boolean maintains a state (any state) for 30 seconds.

  - platform: state
    entity_id: input_boolean.summer_mode
    for: 00:00:30

Is that the behavior you want?


Was the input_boolean created via the UI or did you define it using YAML? If you used YAML, did you specify an initial_state? (Same question as Mariusthvdb)

Yes because if you look I do an if else in the action, but it is probably also being triggered when the state is already off and its being set again to off, like a restart, which I don’t want. So I added the 2 on/off states as in the 2nd automation I posted.

This:

  - platform: state
    entity_id: input_boolean.summer_mode
    for: 00:00:30

does not behave the same way as this:

  - platform: state
    entity_id: input_boolean.summer_mode
    from: "off"
    to: "on"
    for: 00:00:30
  - platform: state
    entity_id: input_boolean.summer_mode
    from: "on"
    to: "off"
    for: 00:00:30

The second example looks for a state-change from one value to another and then that new value must persist for at least 30 seconds.

The first example doesn’t need a state-change. It simply requires any state to persist for at least 30 seconds. Restart Home Assistant, the input_boolean is restored to its previous state, that state remains unchanged for 30 seconds, and that’s sufficient to cause it to trigger.

Right, that’s why I made the change. I didn’t realize a restart of the server does indeed trigger state changes, just the “change” is still the same state.

Either you didn’t express your thoughts properly or you misunderstood my explanation because that’s not what is causing the first example to trigger.

The input_boolean’s state value remains the same as it was prior to the restart. The first example’s State Trigger isn’t concerned with state-changes, it simply requires a state to persist for 30 seconds (and that countdown begins at startup because the State Trigger starts fresh on startup).

FWIW, you can also use this (instead of two triggers):

    platform: state
    entity_id: input_boolean.summer_mode
    from:
      - 'on'
      - 'off'
    to:
      - 'on'
      - 'off'
    for: '00:00:30'

It looks for any combination of state-changes and doesn’t trigger on startup (I tested it).

Ok, so are you saying had I not had that 30 second timer, it wouldn’t have triggered on the restart? That timer is there just incase I fat finger the toggle and I don’t want to ever accidentally turn it on/off.

It wouldn’t; try it and prove it for yourself. An automation with the following simple State Trigger doesn’t trigger on startup:

    platform: state
    entity_id: input_boolean.summer_mode