A very simple automation, trigger question (probably)

I want to trigger an automation when the current time is the same as that held in a sensor. I have tried this and as many variations of it as I can think of but can’t get it to work. Can anyone help please?

  trigger:
    platform: template
    value_template: "{% if is_state('state(sensor.time)', 'state(sensor.my_start_time)') %}true{% endif %}"

Thanks

Try the following:

  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.my_start_time.state }}'

Hope this helps.

1 Like

Thanks for that, a much simpler syntax than I have been led to believe was possible! However whilst I can get it to work with other entities, I am still struggling with mine. Here is the sensor…

- platform: template
  sensors:
    my_start_time:
      friendly_name: "Start time"
      value_template: '{{ "{:02d}".format(states.input_number.start_time_hour.state|int) }}:00'

It’s value comes from an input_number and is formatted as hh:00 (which is true as it is displayed on a card).

Sorry if I am being a bit dim here, although I am new to HA and yaml I do have a long history of coding more traditional (old fashioned) languages!

Use a condition and check every x minutes if the condition is met, like this:

  trigger:
    platform: time
    minutes: '/5'
    seconds: '0'
  condition:
      condition: template
      value_template: '{{ states.sensor.time.state == states.sensor.my_start_time.state }}

If I were you, I would even define an input_select with the possible hours (and :00 as minutes) and then use it directly for comparation, that works.

You can do as @Bob_NL suggests though, as using templates as triggers can be cumbersome.

Thank you both and I have made some progress (intellectually at least!) but I don’t understand why this works:

  trigger:
    platform: time
    minutes: '/1'
    seconds: '0'
  condition:
    condition: template
    value_template: '{{states.input_select.my_start_time.state == states.input_select.my_start_time.state}}'
  action:
    service: homeassistant.turn_on
    entity_id: switch.test_sonoff     

(I know am comparing an input_select with itself but this is just to make it trigger for testing)

but this doesn’t:

  trigger:
    platform: template
    value_template: '{{states.input_select.my_start_time.state == states.input_select.my_start_time.state}}'
  action:
    service: homeassistant.turn_on
    entity_id: switch.test_sonoff

Honestly I don’t know, because template triggers should be very aggressive on the way these are checked:

Try changing some other device state to check if it triggers.

This is seriously overcomplicating everything, if you’ll permit me to say so.

You want to pick an hour from an input_number and correlate that to the time your automaton triggers? Do you only want to trigger at the top of a given hour, or do you want to be able to set an actual time?

If you want to be able to set the time, use input_datetime, then use the trigger as per the docs…

If you do only want the trigger on the hour, and specifically want to use a slider, then you just need to set your trigger like this…

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

Or something very similar, with no need for a template sensor.

Double check the template in the template tool on your interface as I’ve written that on my phone.

Hope this helps.

Thanks for all the replies, I really appreciate it but this is driving me crazy. I must be missing something very obvious because I cannot get a value_template trigger to work at all. (And @anon43302295 I’ll permit you to say anything!)

I have simplified everything to go back to basics.

This doesn’t work:

  trigger:
    platform: template
    value_template: "{{states.device_tracker.steve_phone.state == 'home'}}"
  action:
    service: homeassistant.turn_on
    entity_id: switch.test_sonoff

but this does, so it is not a problem with my action:

trigger:
  platform: time
  seconds: '/5'
action:
  service: homeassistant.turn_on
  entity_id: switch.test_sonoff

and the device tracker state is = home

image

That will trigger when the state changes to home, it won’t trigger just because it is home.

Templates are evaluated when the state of the entity changes, or when checked as a condition.

So that template is the equivalent of

trigger:
  platform: state 
  entity_id: device_tracker.steve_phone
  to: 'home'

But, to be technically correct, what you’re actually doing with that template is

trigger:
  platform: state 
  entity_id: device_tracker.steve_phone
condition:
  condition: state 
  entity_id: device_tracker.steve_phone
  state: 'home'
1 Like

Ah! thanks… Yes, it was obvious, especially as I think I remember reading that before. My problem has been thinking of a trigger as an ‘If’ statement…

1 Like