Noob, but enthusiastic HA user, struggling with template

With version v0.114 the automation below was working fine but after updating to v1.115 it isn’t working anymore. It has something to do with the template and I think I have to fix it with input_datetime but I don’t know how to get the data from the sensor.kerstboom_uit_tijd in the input_datetime entity.

Is there someone who can point me in the right direction ?

automation:

  - alias: kerstboom_aan_bij_zons_ondergang
    initial_state: true
    trigger:
      - platform: sun
        event: sunset

    action:
      - service: homeassistant.turn_on
        entity_id: switch.sonoff_1
  
  - alias: kerstboom uit
    initial_state: true
    trigger:
      platform: template
      value_template: '{{ states.sensor.time.state == states.sensor.kerstboom_uit_tijd.state }}'
    
    action:
      service: homeassistant.turn_off
      entity_id: switch.sonoff_1

If you put ‘states.sensor.time.state’ into the Template Developers Tool you get the following i.e. hh:mm:

I don’t know what kind of sensor ‘kerstboom_uit_tijd’ is but you can start by checking that it is comparable by also copying this to the Template Developers Tool. If it is not also hh:mm then the comparison will never work.

1 Like

Some things recently changed with templates that I don’t fully understand. In general it strikes me as better to trigger on a time pattern, such as every five minutes, and have a condition that checks if the time is right. I am assuming that you are not setting your Christmas lights to be on at some time like 18:32:15. :slightly_smiling_face:

1 Like

Why do you think that’s better than this:

trigger:
  - platform: time
    at: '06:45'

or this:

trigger:
  - platform: time
    at: input_datetime.bedroom_alarm_clock_time

or even this?

    trigger:
      platform: template
      value_template: "{{ states('sensor.whatever') == states('sensor.time') }}"

Confirm the state of sensor.kerstboom_uit_tijd displays time in this format HH:MM like 06:45 or 21:30 which is the same format as sensor.time. If it displays time as HH:MM:SS then it will never match the time reported by sensor.time.

If it does include seconds in the time, you can fix it like this:

      value_template: '{{ states.sensor.time.state == states.sensor.kerstboom_uit_tijd.state[:5] }}'
2 Likes

I don’t think that it is better than triggering on an input helper datetime, I just forgot about that new feature. I bet that will suite OP nicely! I need to update automations to use that myself.

And yes, hard-coded time, if it works, is best! One less “part.” I assumed that it doesn’t fit the use case though.

@Jonah1970, @Dolores, @123 Thank you for the information, this makes it really clear to me. Thanks.

2 Likes