Help with trigger

I am trying to create an automation to trigger ahead of a certain time that I set. I have the following config, but the automation won’t trigger even though I have verified that
{{ (as_timestamp(now()) + states.sensor.seconds_in_advance.state | int) | timestamp_custom("%H:%M") == states.sensor.warm_time.state }}
does in fact return ‘True’ or ‘False’.

    name: Tim
    icon: mdi:timer
    min: 6
    max: 23
    step: 1
    name: Min
    icon: mdi:timer
    min: 0
    max: 45
    step: 15

  platform: template
      friendly_name: 'Bilen varm'
      value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.warmhour") | int, states("input_number.warmminutes") | int) }}'
      value_template: >
        {%- if states.sensor.popp_zweather_temperature.state | float <= -15 -%}
        {%- elif states.sensor.popp_zweather_temperature.state | float <= -8 -%}
        {%- elif states.sensor.popp_zweather_temperature.state | float <= -3 -%}
        {%- elif states.sensor.popp_zweather_temperature.state | float <= 2 -%}
        {%- else -%}
        {%- endif -%}

  - alias: Starta värme i förväg
    initial_state: on
      platform: template
      value_template: '{{ (as_timestamp(now()) + states.sensor.seconds_in_advance.state | int) | timestamp_custom("%H:%M") == states.sensor.warm_time.state }}'
      condition: and
      - condition: template
        value_template: '{ states.sensor.seconds_in_advance.state != 0 }'
      - condition: state
        entity_id: input_boolean.auto_heat
        state: 'on'
    - service: homeassistant.turn_on
      entity_id: switch.ysg258_heater

Could anyone help me out?

first and foremost, they have a relatively new ui control called input_datetime. You should use that, may be easier than all your string conversions.


{{ (as_timestamp(now()) + states.sensor.seconds_in_advance.state | int) | timestamp_custom("%H:%M") == states.sensor.warm_time.state }}

This will never be true. Now is always right now and you are adding to it. If you add to now, it will always be moving the amount you have in the future, thus never reaching it.

A simple solution to this is to subtract your seconds_in_advance from your warm_time. Then, you just check to see if now is equal to warm_time. This of course, assumes warm_time is in the future.

Thanks for the feedback. I did try input_datetime a while ago, but found it difficult to use because it also includes date when I only need the time. Omitting the date part of it never worked for me, so I stuck with my previous time/string setup which works well in other automations.

{{ (as_timestamp(now()) + states.sensor.seconds_in_advance.state | int) | timestamp_custom(“%H:%M”) == states.sensor.warm_time.state }}

This will never be true. Now is always right now and you are adding to it. If you add to now, it will always be moving the amount you have in the future, thus never reaching it.

I can’t really see why not. As I am dealing with only hours and minutes, this should be true when now() + e.g. 600 seconds formatted into HH:MM is equal to my warm_time (also expressed as HH:MM). When I try it out manually in the UI templates section of HA, I get the value ‘True’ when I add the number of seconds that is equivalent to the delta between now() and the future time (warm_time). It should be true until now() skips to the following minute. If it was comparing the whole time value, I could understand that it could only be true at a very small fraction of a second.

I wanted to do it the other way around at first, but I couldn’t find out how to convert my warm_time (HH:MM) into a time value, from which I could subtract the required amount of seconds to arrive at the trigger time.

Ah ok, I miss understood what you were doing. You may need to trigger this every minute with the value_template in the condition.

I don’t know if the trigger will actually evaluate periodically with the value_template in the trigger. It may be looking for a state change, and if the only object that you have that ‘updates’ in your value_template is states.sensor.warm_time.state. So I could see this only evaluating when that state changes.

I was merely trying to mimick the following automation, which triggers fine every time:

  - alias: Väckning vardagar
    initial_state: on
      platform: template
      value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
    - condition: state
      entity_id: input_boolean.alarmweekday
      state: 'on'
    - condition: state
      entity_id: binary_sensor.veckodag
      state: 'on'
    - service: script.turn_on
      entity_id: script.sovrum_wakeup_lights

Right, but you have 2 sensors that are updating states. state updates initiate the trigger. now() is a datetime object that does not contain the required home assistant properties to make a trigger. datetime is inherit in python. So the trigger is going to ignore that and look at the only thing that changes state, states.sensor.warm_time.state.

I see. Would it work if I made
{{ (as_timestamp(now()) + states.sensor.seconds_in_advance.state | int) | timestamp_custom("%H:%M") }}
into a sensor?

Yes, I think so. It depends on how the sensor template evaluates. If that evaluates on state changes, then it wouldn’t work.

Are you against firing the check trigger every minute?

automation 3:
    platform: time
    # You can also match on interval. This will match every minute
    minutes: '/1'
    seconds: 00

I read somewhere, think it was in an alarm clock thread, that it puts unecessary load on the RPi.

That could be the case, but I doubt that it’s anything significant if you have a rpi3. Seeing that all of your seconds_in_advance are in increments of 10 minutes, you could just run it every 10 minutes. That would only be 144 checks a day.

The docs explicitly mention you can use it to only select a time.

I would do this:

  1. Create a template sensor that computes a delay based on the temperature. This will recalculate based on state change of temperature.
  2. Create input_date sensor for input of the time you need
  3. Create automation for template platform that triggers on “now() = (input_date - delay)”
1 Like

For some reason I can’t get the automation to trigger with the above either. I guess I must look for the problem elsewhere.

I found the culprit preventing my automation from triggering. Missing {} in my condition above.