Template trigger does not work

Yes the whole condition will, not the value template. Read the documentation again.

pulled directly from the documentation:

You can ONLY process the value of the state, not provide a true/false response.

EDIT: I.E. Anything in value_template template {{}} has to resolve to a numerical result, either a float or an integer.

I don’t understand why you are arguing when you have the solution in front of your face. I’ll post it one more time. Maybe you’ll try it this time instead of shrugging it off.

- alias: Bedroom temperature sensor has old data
  trigger:
    - platform: time
      minutes: '\1'
      seconds: 00 #this is needed
  condition:
    - condition: template
      value_template: > 
        {% if states.sensor.soverom_temperature.last_updated is defined %}
          {{ (now() - states.sensor.soverom_temperature.last_updated).seconds / 60 >= 5 }}
        {% else %}
          False
        {% endif %}
  action:
    - service: notify.pushover
      data:
        title: "Sensor warning"
        message: "Temperaturmåling på soverommet er {{ ((now() - states.sensor.soverom_temperature.last_updated).seconds / 60) | int }} minutter gammel"

Well, I see why you’re arguing because I for some reason thought your original result inside the value template was returning true/false when it was actually returning the number of minutes. Which should work. My mistake. Sorry.

So that means your problem lies in your trigger, which is probably because you are missing:

seconds: 00

But this is of course assuming that now() exists inside the value_template environment of the numeric_state. Which it should.

Anyways, if that doesn’t work at all, the value template I posted above will work.

Hey, no problem :slight_smile: I really appreciate that you took the time to help me out. I finally made everything work as I wanted. Here’s the working solution to get an alert when the temperature sensor hasn’t sent data for a while (e.g empty battery):

First the intermediate sensor:

sensor:
  - platform: time_date
    display_options:
      - 'time'   ## Sensor that updates every minute

Then the automation:

- alias: Bedroom temperature sensor has old data
  trigger:
    platform: numeric_state
    entity_id: sensor.time
    value_template: "{{ (now() - states.sensor.soverom_temperature.last_updated).seconds / 60 }}"
    above: 5
  action:
    - service: notify.pushover
      data:
        title: "Sensor warning"
        message: "Temperaturmåler på soverommet sender ikke data"

It will send one notification when data is old, and not repeat it.

2 Likes

Of course, my bad. I messed up thinking this was a property the sensor gives.

Your trigger is weird because you base it on the entity “sensor.time”, but you don’t actually use this sensor in your template ?

I only use sensor.time to trigger the automation once every minute. Then I use the value_template: to create the actual value that is checked with above:. This way I don’t have to use a separate platform: time trigger.

Ah I see why it works then !
sensor.time is a pain because it contains a string HH:MM:SS, not a timestamp. You’d have to convert it if you want to test something else beyond “==”. The workaround you found makes it easier !

I got the hint from Template - Home Assistant :

Useful entities to choose might be sensor.date which update once per day, or sensor.time which updates once per minute.
Note: Time & Date Sensors used as an update trigger, must be configured. If a template uses more than one sensor they can be listed.

can you confirm this is actually working as you describe?
regular time triggering for an automation would be:

  trigger:
    platform: time
    minutes: '/1'
    seconds: 00

Yeah, it works perfectly. I try to keep my config consise and structured, so I’m quite happy with the result.

The (now() - states.sensor.soverom_temperature.last_updated).seconds trick was nice too, not having to rely on as_timestamp().

well, if it works for you, keep it that way. Cant help wondering why it does though.
Wouldn’t you care to try this too:

- alias: Bedroom temperature sensor has old data
  trigger:
    platform: state
    entity_id: sensor.soverom_temperature
  condition:
    condition: template
    value_template: >
      {{ (now() - states.sensor.soverom_temperature.last_updated).seconds / 60 >= 5}}
  action:
    service: notify.pushover
    data:
      title: "Sensor warning"
      message: "Temperaturmåler på soverommet sender ikke data"

doesn’t need to run every minute does it? only when above 5, will consequently evaluate the condition, and if true, notify

It works because sensor.time changes every minute, and that makes the trigger activate. Then it evaluates the value_template clause, and matches it against the below condition.

5 would work as well, but sensor.time gives me 1, and that works fine. The way I see it your trigger would actually fire more often (on every change in sensor.soverrom_temperature).

I’ll see if I can try your version. Don’t keep your hopes up, though, because I’ve used WAY too much time on this already :slight_smile:

im not hoping for anything, simply trying to help create a better automation…

If that were the case, this whole automation would be superfluous, you’re testing if it has older data than 5 minutes are you? If you expect this to fire more often than that, then why create a notification for when it doesn’t.

Any case, fifths works as you say it does, it is undocumented (which is quite possible) and a nice find.
I fear though you are referring here Template trigger does not work - #28 by henrik242 to the new setup of the template sensor, and not anything related to automation triggering.

You’re misreading me :slight_smile:

I believe it works like this:

My version:

  1. automation triggers every time sensor.time changes (i.e. every minute)
  2. it calculates the value_template
  3. it compares the value_template result with above: 5
  4. if the result is true, and it previously was false, send notification

Your version:

  1. automation triggers every time sensor.soverom_temperature changes (can be a few seconds apart)
  2. it calculates the value_template inside the template condition
  3. if the result is true, and previously was false, send notification

So the result is the same, but the load on the server is ever so slightly higher with your version.

Anyway, it doesn’t matter which version I choose, since the cpu impact is probably not noticable at all.

Thanks for the input!

Maybe here somebody know how to fix my problem… With template trigger do not work…

# Ugasni SCHRACK ventilacijo - KABINET: Če je temperaturni senzor manj kot 23 stopinj
- id: '3002'
  alias: SCHRACK - ugasni VENTILATOR
  trigger:
#    platform: numeric_state
#    entity_id: sensor.schrack_t
#    below: '23'
  platform: template
  value_template: "{{ states('sensor.schrack_t') | int < states('input_number.variable_kabinet_ugasni_schrack_ventilator') | int }}"
    for:
      hours: 0
      minutes: 30
      seconds: 0
  condition:
      - condition: state
        entity_id: switch.schrack_fan
        state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.schrack_fan  # Use the actual name of your switch here

Do anybody know how to fix that? Is the problem in “for” statement? If yes, how to use that in value_template…

Thank you…

indent your whole trigger

Sensor via input_number…


# SCHRACK - prižgi VENTILATOR (več kot 25)
    variable_kabinet_prizgi_schrack_ventilator:
      name: Nivo vklopa
      min: 15
      max: 30
      step: 0.1
      unit_of_measurement: "°C"

I solve problem via binary_sensor.

# SCHRACK ventilator - prižgi/ugasni
  - platform: template
    sensors:
      schrack_prizgi_ventilator_boolean:
        friendly_name: "SCHRACK - prižgi ventilator (stanje)"
        icon_template: mdi:gate-and
        value_template: "{{ states('sensor.schrack_t') | int > states('input_number.variable_kabinet_prizgi_schrack_ventilator') | int }}"

And then via binary sensor I trigger automation…

# Prižgi SCHRACK ventilacijo - KABINET: Če je temperaturni senzor več kot 25 stopinj
- id: '3001'
  alias: SCHRACK - prižgi VENTILATOR
  trigger:
#    platform: numeric_state
#    entity_id: sensor.schrack_t
#    above: '25'
    platform: template
    value_template: "{{ is_state('binary_sensor.schrack_prizgi_ventilator_boolean', 'on') }}"
    for:
      hours: 0
      minutes: 15
      seconds: 0
  condition:
      - condition: state
        entity_id: switch.schrack_fan
        state: 'off'
      - condition: state
        entity_id: binary_sensor.schrack_ugasni_ventilator_boolean
        state: 'off'
  action:
    service: switch.turn_on
    entity_id: switch.schrack_fan  # Use the actual name of your switch here

And now it’s working…

Check out how I defined my ‘time’ trigger. I think you will find it a more elegant way to solve you problem.