Dynamic "ABOVE" value in automation trigger

Hi,

I try to create an automation which stops charging of my car above a value which is stored in a variable. I read a lot of in the forms/blogs but I am still not understand how I have to setup this.
I have defined a
helper: input_number.i3_ladegrenze type number:

and have this automation:


alias: i3 bis xx% laden
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: baf89c33574add40c2484a869f931a52
    entity_id: sensor.i3_rex_remaining_battery_percent
    domain: sensor
    above: 75
condition:
  - type: is_charging
    condition: device
    device_id: baf89c33574add40c2484a869f931a52
    entity_id: binary_sensor.i3_rex_charging_status
    domain: binary_sensor
action:
  - type: turn_off
    device_id: 6d1f340497857b27b6bfed6075020c0x
    entity_id: light.016366
    domain: light
mode: single

and I tried to replace the ABOVE value of 75 with the input variable but I failed.
Perhaps someone has an idea how it works. Many thanks in advance - Roland

When you say you “failed”, what was your testing methodology? Numeric state triggers only fire based on the value of the entity in the entity_id field… adjusting the entity used in the above field will not have an immediate effect. The comparison will not be re-evaluated until the state of the battery percentage sensor updates.

If you need it to be re-evaluated when either entity is changed, you can use two Numeric state triggers or a single Template trigger.

They are using a device trigger. I’m not sure it actually supports an entity in the above and below fields like a numeric_state trigger.

It would be wise to stop using device triggers, condition and actions altogether.

I put in the input helper variable but the system complaint that I have to use a float variable. How I can convert the input_number.i3… into a float? Do you have some example coding for me?

You can’t use a device trigger. Use a numeric state trigger.

Hi Tom, Didgeridrew,

many thanks for your help. I changed the trigger but how I have to code that the trigger is true if the input_number.i3_ladegrenze >= sensor.i3_rex_remaining_battery_percent?

Changed coding:

alias: i3 bis xx% laden

description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.i3_rex_remaining_battery_percent
    to: "> input_number.i3_ladegrenze"
    attribute: attribution
condition:
  - type: is_charging
    condition: device
    device_id: baf89c33574add40c2484a869f931a62
    entity_id: binary_sensor.i3_rex_charging_status
    domain: binary_sensor
action:
  - type: turn_off
    device_id: 6d1f340497857b27b6bfed6075020c0d
    entity_id: light.016373
    domain: light
mode: single


For numeric comparisons you use a numeric_state trigger type, not state.

alias: i3 bis xx% laden
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.i3_rex_remaining_battery_percent
    above: input_number.i3_ladegrenze
condition:
  - type: is_charging
    condition: device
    device_id: baf89c33574add40c2484a869f931a62
    entity_id: binary_sensor.i3_rex_charging_status
    domain: binary_sensor
action:
  - type: turn_off
    device_id: 6d1f340497857b27b6bfed6075020c0d
    entity_id: light.016373
    domain: light
mode: single

Hi Drew,

it doesn’t work :frowning:

Visual edit (same as the coding above)

We’re going to need more details… how are you testing?

Post the automation trace

There is not trace. The trigger is not valid and doesn’t start. But why?

I can trigger it manually and then the automation works fine.

For your Numeric state trigger to fire, the value of the battery sensor’s state must change in such a way that the comparison’s evaluation switches from false to true. You cannot just change the input number and expect the automation to fire.

To test if the trigger works, you have two options:

  1. Wait for the battery charge to drop below the your input number through use, then recharge it.
  2. Use the “Set State” function on the Developer Tools > States tool to set the state below your input number, then above.

… and how do I do it? With an additional helper? I’m still haven’t understood why programming HA is sometimes so complicated.

I don’t understand what you are asking here.

Ok, I understand now that I can’t use a state for trigger comparing. The charging percentage is state (in this example 50). How can I convert it into an attribute?

I have the same problem with the Lovelace slider where I’d like to set the limit:

How it is possible to compare this values in a trigger or in a condition. (if sensor… > input… then…)

This is incorrect. States are the most common source of triggers in Home Assistant.

For comparisons, state triggers are used when comparing an entity’s state to an absolute value. Numeric state triggers are used to perform “greater-than” and/or “less-than” comparisons between an entity’s state and a numeric value, which can be supplied by the state of another entity if desired. As stated previously, this type of trigger only fires on changes of the entity listed under the entity_id key.

trigger:
  - platform: numeric_state
    entity_id:
      - sensor.i3_rex_remaining_battery_percent
    above: input_number.i3_ladegrenze

Alternatively, you can use template trigger. This type of trigger works slightly differently. The “greater-than” comparison will be reevaluated every time the state of either entity changes. When one of those state changes causes the evaluation of the comparison to change from “false” to “true”, the trigger will fire and the conditions block will then be evaluated.

trigger:
  - platform: template
    value_template: |
      {{ states('sensor.i3_rex_remaining_battery_percent') | float(0)
      > states('input_number.i3_ladegrenze') | float(0) }}

Hi Drew,
HAPPY BIRTHDAY! :moon_cake: :champagne:
I dried your 1. suggestion a few days ago but it didn’t work (Perhaps I made a mistake). Now I try the template suggestion but the i3 is away for a week. You will get a replay next weekend.
Cheers

Thanks for the great input here Drew, Im picking up on this thread with a similar question. Put differently…

Are you saying (as I suspect) that a state trigger will only trigger once the state passes the set threshold whereas templates dynamically poll the sensor for its ongoing state.

I have a similar situation where I want my geyser to come on when the batteries are above say 60% (using the battery sensor) BUT also want the total PV to be above a level. This means that as the batteries are being charged during the day they may go over the 60% but not meet the set minimum PV condition and therefore will not switch on the geyser, if however later the PV increases because the trigger has already been “passed” no event will trigger ?

Is this the case and in which case should I then opt for a template trigger ?

Thanks

Yes and No… As you wrote, a Numeric state trigger only triggers when the state of the entity crosses the defined threshold and the automation will not wait around for other conditions to be satisfied. In the case of your geyser, the actions would not be executed.

However, a Template trigger doesn’t exactly “poll” the entities’ states, it is simply recalculated every time the state changes.

You can certainly use a Template trigger. Another option is to use two Numeric state triggers with two corresponding Numeric state conditions.