Value template / trigger.to_state.state

Hi all

Am trying to set a value template in a automation

I have 3 tiggers for 3 rooms in the temperature drops below 15 it turns on the heating system

I am now trying to use wait for a trigger using a vale template to turn the heating off once the room that triggered hits 19

< {{trigger.to_state.state > 19 }} >

However it’s not working, could anyone advise how I have messed this up?

{{ trigger.to_state.state | int(0) > 19 }}

EDIT

The assumption is you’re using a Numeric State Trigger to monitor an entity’s state value. It’ll need to be modified if it’s monitoring an entity’s attribute.

1 Like

Thank you for the quick response, I appreciate the help :blush:

It doesn’t appear to be working, it’s still not triggering

Am I missing something? I have checked and it seems to be


Please post the entire automation in YAML format (as formatted text, not an image).


EDIT

At first glance, you can’t use trigger.to_state.state like that in a wait_for_trigger. However, I would like to see the entire automation to get a better understanding of what you are attempting to do.


alias: "Critical temperature alert 12 "
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: 75866cdf8b0681b0dbb9
    entity_id: fdc3cf311e377aa5b567d
    domain: sensor
    below: 15
    for:
      hours: 0
      minutes: 0
      seconds: 30
  - type: temperature
    platform: device
    device_id: 35b98a30d05e64c47ca06
    entity_id: 34ee700e3331f46d03c
    domain: sensor
    below: 12
    for:
      hours: 0
      minutes: 0
      seconds: 30
  - type: temperature
    platform: device
    device_id: d6f19fbf2b424344fa
    entity_id: a2b54e582ddd65c05
    domain: sensor
    below: 14
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition:
  - condition: zone
    entity_id: person.c
    zone: zone.home
  - condition: or
    conditions:
      - condition: zone
        entity_id: person.c
        zone: zone.home
action:
  - action: notify.mobile_app_c
    metadata: {}
    data:
      title: "Critical temperature alert "
      data:
        push:
          sound:
            name: default
            critical: 1
            volume: 1
      message: >-
        The temperature in {{ area_name(trigger.entity_id) }} is at {{
        trigger.to_state.state }} 
    enabled: true
  - action: notify.mobile_app_c
    metadata: {}
    data:
      data:
        data:
          ttl: 0
          priority: high
          channel: alarm_stream
      title: Critical Temperature Alert
      message: >-
        The temperature in {{ area_name(trigger.entity_id) }} is at {{
        trigger.to_state.state }}
  - action: climate.set_temperature
    target:
      entity_id: climate.hallway
    data:
      temperature: 19
      hvac_mode: heat
  - wait_for_trigger:
      - platform: template
        value_template: "{{ trigger.to_state.state | int(0) > 19 }}"
    continue_on_timeout: true
  - action: climate.set_temperature
    metadata: {}
    data:
      hvac_mode: "off"
    target:
      entity_id: climate.hallway
  - action: notify.notify
    metadata: {}
    data:
      message: "Heating turned off "
      title: "Temperature is now {{ trigger.to_state.state }} degrees, "
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "Heating turned off "
      title: "Temperature is now {{ trigger.to_state.state }} degrees, "
    enabled: true
mode: single

Done, thanks for your help

As suspected, that Template Trigger won’t work the way you want. Assuming the automation was triggered by a temperature value of 14, the template would effectively become this:

{{ 14 | int(0) > 19 }}

The result of that is false and it will never become true because nothing changes within the template.

Perhaps this is more along the lines of what you want:

  - wait_for_trigger:
      - platform: template
        value_template: "{{ states(trigger.entity_id) | int(0) > 19 }}"
    timeout:
      minutes: 5
    continue_on_timeout: true

Adjust the timeout value to suit your needs.

The template checks if the state value of whatever entity was responsible for triggering the automation exceeds 19. It waits up to 5 minutes for this to happen (and if it doesn’t, it simply continues anyways because continue_on_timeout is true).

In addition to Taras’ response above, you may need to change your condition block… though it’s hard to tell what you are trying to do from what you have provided. Are there actually two person entities whose states need to be checked?

I can understand @123 your explanation to why it wasn’t working from using the original value and this need to be a live value so thank you for explaining this as it make perfect sense.

However am still having issues and it’s not triggering

@Didgeridrew apologies for my poor explanation

1)It triggers on a room temperature dropping below a certain point
2) checks one of 2 people are home
3) sends a notification to let us know
4)turns heating on
**Working to this point **
5) checks the room that triggered as got to a certain temperature
6) turns heating off
7) sends a notification to let us know

Point 5 seems to be the issue am trying to use a wait for with a Value template* with the above suggestion however is not working

Clearly am doing something wrong :expressionless:

That is not what it is currently set up to do. Currently, both people must be home for the condition to pass.

You have a couple options to accomplish what you are trying to do:

condition:
  - condition: state
    entity_id:
      - person.a
      - person.b
    match: any
    state: "home"
condition:
  - condition: or
    conditions:
      - condition: zone
        entity_id: person.a
        zone: zone.home
      - condition: zone
        entity_id: person.b
        zone: zone.home

You’re correct, I have just corrected that part. Thank you

If either of you have any more ideas or thoughts about how I can achieve step 5, I would appreciate the help :blush:

Please download and post the Trace json file, so we can see what is happening.

Describe your expectations of what should happen versus what is actually happening.

What I suggested above will wait for the temperature to exceed 19 degrees OR timeout after 5 minutes. Either way, it continues executing the balance of the automation.

What is the behavior that you’re observing?

Hi

Thank you to both of you for your support and assistance

I believe I have this working now and it was the way I had it set up. @123 your code seem to be working perfectly

I have changed to use

Wait for a template to evaluate to true

What I had

Wait for a trigger with a Value template

Still leaning what the difference is however the clearly is one

Thank you @Didgeridrew & @123 for you time and explaining how this works. Helps my learning

Post the wait_template you created.

A wait_for_trigger with a Template Trigger is effectively the same thing as a wait_template. Both wait for the template’s result to change from false to true.

I just used this however within a Wait for a template to evaluate to true little confused to why it started worked after I changed it, if they work in the same way, maybe just removing and adding the wait back in make a difference?

Regardless your help got me to the answer I was after and prevented me from having 3 separate automations to do the same thing.

1 Like

These two examples should behave the same way.

  - wait_for_trigger:
      - platform: template
        value_template: "{{ states(trigger.entity_id) | int(0) > 19 }}"
    timeout:
      minutes: 5
    continue_on_timeout: true
  - wait_template: "{{ states(trigger.entity_id) | int(0) > 19 }}"
    timeout:
      minutes: 5
    continue_on_timeout: true
1 Like