I want to receive a notification when the difference between two MQTT temperature sensors is greater than a predefined amount. I am able to receive a notification if one sensor is greater than the other, but not when an offset is set using the condition.
alias: B2 Open Curtain Window Warmer Than Room + Offset V1.1a
description: B2 Open Curtain Window Warmer Than Room + Offset V1.1
trigger:
- platform: numeric_state
entity_id: sensor.b2_temp_window_open
above: sensor.b2_temp_room
for:
seconds: 10
condition:
- condition: template
value_template: "{{ (sensor.b2_temp_window_open.value - sensor.b2_temp_room.value) > 20 }}"
action:
- device_id: 49d9d4d2xxxx
domain: mobile_app
type: notify
message: B2 Open Curtain Window Warmer Than Room + Offset V1.1
mode: single
Using Home Assistant 2022.10.1
The code above seems to ignore the condition.
I have tried the above without the above section, but I received no notification.
I’m sure there is an answer to your question by the smart folks on this forum. That said, a while back I solved a similar problem a different way by creating a new ‘delta’ sensor out of the two temperature sensors and then watching this single numeric value in an automation, working great for 2 years, currently on 2022.9.7:
sensors.yaml
# stove temperature minus ambient kitchen temperature
stove_kitchen_temp_diff:
friendly_name: "Stove To Kitchen Ambient Delta"
value_template: >-
{% set stove = states('sensor.kitchen_stove_govee_h5074_temperature') | float(0) %}
{% set kitchen = states('sensor.kitchen_atc_mi_temperature') | float(0) %}
{{ (stove - kitchen) | round(2) }}
unit_of_measurement: '°F'
automations.yaml
- alias: Stove temperature
trigger:
- platform: numeric_state
entity_id: sensor.stove_kitchen_temp_diff
above: 10
for:
minutes: 60
action:
- service: notify.pushover
data:
message: Stove temperature delta has been above 10ºF for 60 minutes
data:
priority: 1
sound: gamelan
- service: tts.cloud_say
entity_id: media_player.living_room
data:
message: Stove temperature delta has been above 10ºF for 60 minutes
language: en-US
options:
gender: female
Your template condition is not a valid template. Even with that fixed, I don’t think this automation will work the way you seem to want it to work.
Conditions do not wait, if the states of the sensors are not more than 20 degrees apart when the trigger fires the actions will not execute; and the trigger will not fire again until temp_window_open goes below temp_room and then above it again. So, in order for the automation’s actions to be executed, temp_window_open would have go directly from below temp_room to 20 degrees above it.
You can specify a value template within the Numeric trigger in order to get the function you are trying to acheive. Because it is possible to get false triggers on restart or reload of the sensors it is advisable to include a couple conditions as well.
alias: B2 Open Curtain Window Warmer Than Room + Offset V1.1a
description: B2 Open Curtain Window Warmer Than Room + Offset V1.1
trigger:
- platform: numeric_state
entity_id: sensor.b2_temp_window_open
above: sensor.b2_temp_room
value_template: "{{ state.state|float(0) - 20 }}"
for:
seconds: 10
condition:
- condition: template
value_template: >
{{ states('sensor.b2_temp_window_open') | float('unknown') is number }}
- condition: numeric_state
entity_id: sensor.b2_temp_window_open
above: sensor.b2_temp_room
value_template: "{{ state.state|float(0) - 20 }}"
action:
- device_id: 49d9d4d2xxxx
domain: mobile_app
type: notify
message: B2 Open Curtain Window Warmer Than Room + Offset V1.1
mode: single
Thank you Drew for your assistance. I will have a look through your example once I have fixed up my problems with the configuration.yaml and related files.