Using Value Templates in Numeric State Trigger for offset trigger

I have a numeric value helper (“desired_pool_min_temp”)

I’d like an automation to trigger when a sensor’s reported temperature goes above that helper value PLUS an offset. So if if desired_pool_min_temp is 90, i want the automation to trigger when sensor7 goes above 91.

I’ve tried

platform: numeric_state
entity_id: sensor.temp_7
value_template: "{{ state.attributes.value - 1 }}"
above: input_number.desired_pool_min_temp
enabled: true

but it doesn’t seem to work, and i’m sure it’s due to me not understanding templates yet. Help for a newbie, please and thank you.

The way you have it configured, the input number is being compared to an attribute of the sensor named value… is that correct? If you are unsure, go to Developer Tools > States and type in the entity ID to see what attributes are available for that entity.

I thought value was a generic attribute (well, at least that was the example in the docs i copied anyway).

The only attributes in dev tools are

state_class: measurement
unit_of_measurement: °F
device_class: temperature
friendly_name: Pool Temperature

And the actual temperature reading (what i want) is just it’s state. So are you saying it should be

platform: numeric_state
entity_id: sensor.temp_7
value_template: "{{ state - 1 }}"
above: input_number.desired_pool_min_temp
enabled: true

?

value_template: "{{ value|float(0) - 1 }}"

Tried

platform: numeric_state
entity_id: sensor.brynnct_temp_7
value_template: "{{ value|float(0) - 1 }}"
above: input_number.desired_pool_min_temp
enabled: true

Doesn’t seem to ever trigger. Using dev tools to change the reported temp of the sensor.

Show the full automation.

You could also try

value_template: "{{ states('sensor.brynnct_temp_7')|float(0) - 1 }}"

Ok, i got it to work by just doing a template trigger all together.

platform: template
value_template: "{{ states('sensor.brynnct_temp_7')|float(0) > (states('input_number.desired_pool_min_temp')|float(0) +1 )}}"
enabled: true

This works as i want.
Though still i don’t understand when and how the value template line gets evaluated in a numeric state trigger.

The proper configuration to compare the state of the entity is as follows:

platform: numeric_state
entity_id: sensor.temp_7
value_template: "{{ state.state | float(0)  - 1 }}"
above: input_number.desired_pool_min_temp

Or

platform: numeric_state
entity_id: sensor.temp_7
value_template: "{{ state.state | float(0) - states('input_number.desired_pool_min_temp') | float(0) }}"
above: 1

For a Numeric state trigger, the template is evaluated whenever the state of the entity listed under entity_id changes. Unlike a Template trigger, changes to any other entity included in the template or above/below fields will not initiate the trigger.

Update: The Numeric state trigger’s template is no longer limited to updating based on the entity listed under entity_id. Both Numeric State triggers with value templates and Template triggers will be render following the same rules.

5 Likes

This didn’t work either, but my template trigger i posted does, so i know my testing method is sound.

I suspect it’s formatting or syntax of the numeric state of the sensor. This is the sensor in dev tools.

Both of the options I posted have been tested and work.

You will need to describe your testing method. As described in my earlier post, similar Numeric State and Template triggers will not always fire equivalently.

I’ve copied and pasted your YAML, then gone to developer tools to manually changed the sensor (temp7) states value to lower and then high the threshold (before and after the offset) and watched the logbook live. I have a mirrored automation that’s my template trigger to make sure it’s firing. Your suggestion did not work.

Did you make sure to update the entity_id to your entity’s actual ID? That’s my last guess as to why it isn’t working for you.

Where is that documented?

It’s not documented explicitly, it’s only lightly implied by the attribute-based examples in the Automation triggers docs. From there, the docs just expect you to have an understanding of HA state object structure… :man_shrugging:

1 Like

Ugh, I was working on that for too long last night. Good catch, it now works. Thanks for the help making it work and educating me on templating.

Thanks, this helped me as well. What wasn’t clear from the example was that the value template "{{ state.state | float(0) - 1 }}" get’s the state from the entity defined in the trigger condition since this is not explicitly stated.

In my case I wanted to check when my EV state of charge reached the target state of charge (these are 2 different sensors), so for the trigger entity I selected to monitor the entity (using a numerical state automation):

sensor.id_4_gtx_4motion_220_kw_299_ps_state_of_charge

And in the value template I entered:

{{ states('sensor.id_4_gtx_4motion_220_kw_299_ps_target_state_of_charge') | float(0) - state.state | float(0)}}

The condition is when the above value statement is below 1, which means the evaluation is based on “target state of charge - the current state of charge = 0” (they’re the same).

This allows me to change the target state of charge in the car/app and not have to worry about it in the automation.

Can you post your trigger’s configuration…? From what you wrote is sounds like it is as follows:

platform: numeric_state
entity_id: sensor.id_4_gtx_4motion_220_kw_299_ps_target_state_of_charge
value_template: |
  {{ states('sensor.id_4_gtx_4motion_220_kw_299_ps_target_state_of_charge') | float(0) 
  - state.state | float(0)}}
below: 1

There doesn’t seem to be a reference to the current state of charge, so that condition will always be true… It is essentially asking if 0 is less than 1.

Actually, the entity id is the current state of charge, not the target state of charge:

sensor.id_4_gtx_4motion_220_kw_299_ps_state_of_charge

This is the full automation:

- id: '1708508416748'
  alias: Turn off right ChargeAmps connector when ID4 reaches target charging state
  description: Turn off connector to prevent ID4 heating to draw power during peak
    hours
  trigger:
  - alias: When ID4 GTX reaches state of charge for 5 minutes
    platform: numeric_state
    entity_id:
    - sensor.id_4_gtx_4motion_220_kw_299_ps_state_of_charge
    value_template: '{{ states(''sensor.id_4_gtx_4motion_220_kw_299_ps_target_state_of_charge'')
      | float(0) - state.state | float(0)}}'
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 1
  condition:
  - condition: state
    state: 'on'
    entity_id: switch.hemma_2102004640a_2
  action:
  - service: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id:

Maybe I should change the value template to make it super clear what I’m evaluating? Something like this? Would both statements work?

 value_template: {{ states('sensor.id_4_gtx_4motion_220_kw_299_ps_target_state_of_charge') | float(0) - states('sensor.id_4_gtx_4motion_220_kw_299_ps_state_of_charge') | float (0)}}

The issue I was trying to clarify seems to have just been a typo in your earlier post… you used the “target_state_of_charge” entity ID in both places in your description. But your actual trigger looks fine. You can use either of the template variations you posted, it shouldn’t change the functionality appreciably.

Great! I edited the original post to avoid confusion from future visitors, thanks for spotting the error!