Automation Value Template Formatting Help

Hi all - trying to figure out why my automation trigger is misbehaving.

- id: '1564531968523'
  alias: TV Backlight On
  trigger:
  - platform: template
    value_template: "{% if not is_state('sensor.tv_room_harmony_hub', PowerOff)
      and is_state('sun.sun', 'below_horizon') %}true{% endif %}"
  condition:
  - condition: state
    entity_id: switch.tv_backlight
    state: 'off'
  action:
  - data:
      entity_id: switch.tv_backlight
    service: switch.turn_on

I would like to turn on my TV Backlight whenever my Harmony Hub is not in the “PowerOff” state, and the sun goes down.

What it currently does is just trigger as soon as the sun goes down, even if the Harmony Hub is in the PowerOff state.

Try with single quotes around the tested hub state ( 'PowerOff')

You can also change the trigger template to this equivalent:

    value_template: "{{ not is_state('sensor.tv_room_harmony_hub', 'PowerOff')
      and is_state('sun.sun', 'below_horizon') }}"

As this will evaluate to true/false without needing to explicitly state it.

Other things to check:

  • Is the automation enabled?
  • What do the states of the sensors and switch show in the developer tools states menu when you expect it to trigger?

Thanks for the reply! Stupid of me to not have quotes around PowerOff; I’ve added them now.

The automation is enabled.

The states are as follows:

Entity                          State           Attributes
sensor.tv_room_harmony_hub      Watch Roku      friendly_name: Harmony Hub
sun.sun	                        below_horizon	next_dawn: 2019-08-03T10:09:17+00:00
                                                next_dusk: 2019-08-04T01:02:55+00:00
                                                next_midnight: 2019-08-03T05:36:02+00:00
                                                next_noon: 2019-08-03T17:36:06+00:00
                                                next_rising: 2019-08-03T10:37:15+00:00
                                                next_setting: 2019-08-04T00:34:57+00:00
                                                elevation: -29.99
                                                azimuth: 326.95
                                                rising: false
                                                friendly_name: Sun

I think the automation should definitely trigger given those states, but for some reason it won’t!

If the sensors are already in those states it wont trigger. Either the sun needs to change from above to below horizon or the harmony hub state has to change (to anything but PowerOff) for the automation to trigger. Try changing the harmony hub state.

Just for fun, try this version:

- id: '1564531968523'
  alias: TV Backlight On
  trigger:
  - platform: state
    entity_id: sensor.tv_room_harmony_hub
  condition:
  - condition: template
    value_template: "{{ states('sensor.tv_room_harmony_hub') != 'PowerOff' }}"
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  - condition: state
    entity_id: switch.tv_backlight
    state: 'off'
  action:
  - data:
      entity_id: switch.tv_backlight
    service: switch.turn_on

Interesting - so we move the triggers to conditionals instead?

I think the problem with that one would be if we’re already watching TV, and then the sun goes down. I would need another automation that triggers on the sun.sun changing states as well, correct?

The goal of this version of the automation was to simply see if it works. If it does, that’s encouraging, and the next step would be to shift important bits from the condition into the trigger and still keep it working.

So … does it work as posted above?

So, the automation works great once I added the single quotes around PowerOff. I feel stupid to have left that out!

value_template: "{% if not is_state('sensor.tv_room_harmony_hub', 'PowerOff')
  and is_state('sun.sun', 'below_horizon') %}true{% endif %}"

@123 I can still try your version out if you’d like me to.

If what you have now works, then there’s no pressing need to try my version.

I had offered it only because I was under the impression you had added the missing quotes to PowerOff yet it still failed to work.

Perhaps I misunderstood what you meant.

Ah, I see where the confusion came from then! I was under the impression that it would check the states periodically to evaluate the value_template. Unfortunately, it seems that it only checks it if a state changes. The sun had already set, and the tv remote was already in the ‘Watch Roku’ state when I updated my script, so it did not trigger. @tom_l’s reply was correct.

Once I tried toggling the tv remote state to PowerOff and then to something else, the automation triggered and all was good. :slight_smile:

You guys are great for helping me out - thanks again!