Automation does not work on value of input_boolean where value was set with a service call in automation

I have a bit of automation (turning pond filters on or off) that doesn’t work if the value of the input_boolean has been set by this automation, but DOES work when the values are set through the UI. This the code (sample used in testing this behavior):

#########################################
# Check every hour if filter needs to be#
# turned on or was triggered manually   #
#########################################
- alias: Vijver filtering aan - triggering 
  trigger: 
    - platform: time
      minutes: '/2'
      seconds: 05
    - platform: event
      event_type: button_pressed
      event_data: {"state": "on", "entity_id": "switch.klik42"}
  condition:
    - condition: and
      conditions:
      - condition: state
        entity_id: input_boolean.pond_activate
        state: 'off'
      - condition: state
        entity_id: input_boolean.pond_active
        state: 'on'
      - condition: or
        conditions:
        - condition: state
          entity_id: switch.klik42
          state: 'on'
        - condition: template
          value_template: "{{ now().hour == states('input_number.pond_on') }}"
  action:
    - entity_id: scene.vijver_actief
      service: scene.turn_on
    - entity_id: input_boolean.pond_activate
      service: input_boolean.turn_on

#########################################
# Check every hour if filter needs to be#
# turned off or was triggered manually  #
#########################################
- alias: Vijver filtering uit - triggering
  trigger:
    - platform: time
      minutes: '/3'
      seconds: 05
    - platform: event
      event_type: button_pressed
      event_data: {"state": "off", "entity_id": "switch.klik42"}
  condition:
    - condition: state
      entity_id: input_boolean.pond_activate
      state: 'on'
    - condition: or
      conditions:
      - condition:  state
        entity_id: switch.klik42
        state: 'off'
      - condition: template
        value_template: "{{ now().hour == states('input_number.pond_off')}}"
  action:
    - entity_id: input_boolean.pond_activate
      service: input_boolean.turn_off

The input_boolean in question is pond_activate, if it is turned on by the first rule (last action, reflected in UI), the second will never execute (first condition is to check if this switch is on), and if it is turned off by the second rule the first will never get executed (first condition is to check if switch is off). however, if I toggle this switch in the UI then the automation does get executed (off when it was on or on if it was off). Checking the states in hass show the correct state, so I am at a loss why this doesn’t work.

Addendum, setting the switch with a service call in the interface does work ?!?

please follow the guidelines at the top of the page to properly format your code so people can read it:

Your second automation has an and that you may not be aware about:

Your condition on the second reads as:

if ( input_boolean.pond_activate is on ) and ( ( switch.klik42 is off ) or ( the current hour equals input_number.pond_off) )

so every 3 minutes, this will only trigger if input_boolean.pond_activate is on and the hour is correct.

or

whenever off is pressed and input_boolean.pond_activate is on.

I am aware of that in testing this I set pond_on and pond_off to the current hour. the interval was just to check if I could flip/flop the switch. In testing it was revealed that if the value of pond_activate was set by the automation (by a service call in action) itself it would never work, as soon as I set the value with the UI or a service call in the UI it worked. So I am inclined to believe I hit a bug somehow, also because when I checked the values in the interface they appeared to have been set properly.

So what is your overall goal with these 2 automations. Every time I look at this, I see over complication. Especially with the intervals. But you said here:

So if you remove the intervals, the first automation will never fire the second automation because the only trigger that is left is the ‘switch.xxx to off’.

So lets take a step back and see what your end goal is. What do you want these automations to do?

Do you want them to only turn on the pond … wait a period… and turn off the pond, only from a switch?

The objective is to start pond filtering at pond_on hours or (when manually started with Klik42) and the reverse with pond_off. I worked before with timers and it gave me GREAT pains in the back, hence this construction. Further testing has revealed what the problem actually is. The problem is not in the value ( or the setting thereof) of pond_activate but in the hour comparison: now().hour returns an INT and states(‘input_number.pond_on’) returns a float and the template does not consider int 18 the same as float 18.0 After casting states(‘input_number.pond_on’) to INT it worked as desired and expected. so the value template that is working is like this:

      - condition: template
        value_template: "{{ now().hour == (states('input_number.pond_on') | int) }}"

So problem / mystery resolved.

I still think it’s over complicated and I believe you’ll be taxing your system with constant automation firing. Simply adding the date time sensor and using the for: xxx will be all that you need. You could even use a input_datetime to make it even easier.

input_datetime:
  pond_on:
    name: Pond Time
    has_time: true

sensor:
  - platform: time_date
    display_options:
      - 'time'

#########################################
# turned on or was triggered manually   #
#########################################
- alias: Vijver filtering aan - triggering 
  trigger:
    - platform: template
      value_template: "{{ states('input_datetime.pond_on') == states('sensor.time') }}"
    - platform: state
      entity_id: switch.klik42
      from: 'off'
      to: 'on'
  action:
    - service: scene.turn_on
      entity_id: scene.vijver_actief

#########################################
# turned off or was triggered manually  #
#########################################
- alias: Vijver filtering uit - triggering
  trigger:
    - platform: state
      entity_id: switch.klik42
      to: 'on'
      for:
        hour: 1
  action:
    - service: switch.turn_off
      entity_id: switch.klik42

No crazy conditions. No timers. Simple automations.

Thanks for the suggestion, looks a lot simpler, I’ll give that a whirl.:smiley:

1 Like

Well… That didn’t work either, in testing I see states(‘input_datetime.pond_on’) returning 18:00:00 and states(‘sensor.time’) returning 18:00 which are not considered equal by the template ?!?. Think go back to my hour construction, at least that worked.

that’s a simple formatting issue though and it’s simple to correct.

value_template: "{{ states('input_datetime.pond_on') == states('sensor.time')+':00' }}"

or a safer route would be:

value_template: "{{ state_attr('input_datetime.pond_on','timestamp') | int | timestamp_custom('%H:%M', False) == states('sensor.time') }}"