Add Offset to Value in GUI Automation

Trying to add an Offset to Values in an Automation, Preferably in the GUI Editor.
Have searched, but not finding current examples & would rather do it in the GUI if possible & avoid adding it as a template.

Automation is to turn on a Fan & Open HVAC Dampers when Roof Temp is 2°C above Lounge Room them.

AI seems to be sending me down all the wrong roads.

This is the basic example, & is it possible in the YAML to add 2 to the above: value?
As in sensor.hvac_1_ds18b20_3_l_room + 2 .

Have tried a few things in the Developers tools, but can't get a valid entry for the GUI Yaml.

Thanks.

According to the documentation for the Numeric State Condition, its above and below options accept only two kinds of values, a number or an entity_id. To perform arithmetic, the two options would need to support the use of templates, but they do not.

The Numeric State Condition has a value_template option that lets you alter the value that is being compared to above and below (i.e. the value of entity_id) with a template. However, it doesn't help in this situation because you want to increment the value of above not the value of entity_id (unless you don't mind subtracting 2 from the entity_id's value).

Subtract 2 instead of add 2
condition: numeric_state
entity_id: sensor.hvac_1_ds18b20_4_roof
value_template: "{{ float(state.state) - 2 }}"
above: sensor.hvac_1_ds18b20_3_1_room

For your application, I suggest you use a Template Condition.

condition: template
value_template: "{{ states('sensor.hvac_1_ds18b20_4_roof') | float(0) > states('sensor.hvac_1_ds18b20_3_1_room') | float(0) + 2 }}"


How it works

  • states() is a function that gets the state value of a given entity_id.

  • An entity's state value is always a string, even when it appears to be a number. If we intend to use it to perform arithmetic, or compare it to another number, we need to convert it from a numeric string to an actual number.

  • float() is a function that converts a numeric string into a floating-point number. There's a similar function named int() for converting numeric strings into integers.

  • The zero in float(0) is a default value. If float cannot convert a string into a number, for example the entity's value is the string unavailable, it will report the default value which, in this case, will be zero. You can set the default value to whatever is best for your application.

It can be done by using the value_template configuration option Taras mentioned above.... remember from your school days that the subtractive property applies to the other numeric comparisons just like it does for equality.

#If this is true
roof > room + 2

#So is this
roof - room > 2
condition: numeric_state
entity_id: sensor.hvac_1_ds18b20_4_roof
above: 2
value_template: |
  {{ state.state | float(0) - states('sensor.hvac_1_ds18b20_3_l_room')|float(0) }}

While this does work, I find the Template condition option in Taras' post both easier to understand and more compact. And, if you were using this in a Trigger instead of a Condition, the Template version is also the better choice due to the way Numeric State triggers update.

Thanks,

I found both Example to work in reverse to what I expected.

But that turn out to be a Typo in both examples above.

The Entity:-

sensor.hvac_1_ds18b20_3_1_room

has a lower case "L" before the "_room"; Short for Lounge Room.
So correct Entities are:-

condition: template
value_template: "{{ states('sensor.hvac_1_ds18b20_4_roof') | float(0) > states('sensor.hvac_1_ds18b20_3_l_room') | float(0) + 2 }}"

Thanks for the Prompt & Well Detailed Replies.

1 Like