Notification automation on every 5 degree (Celcius) temperature step

I’m attempting to create automation that would notify me whenever temperature has changed 5 degrees (in Celcius).

The notification parts is working and also when I’m testing my launch trigger in Developer tools → templates it appears to be correct but it seems I have missed something, as the automation is never triggered.

Here’s the automation:

alias: outtemp
description: ""
trigger:
  - platform: template
    value_template: "{{ (states('sensor.outside')|int(0))%5 == 0 }}"
condition: []
action:
  - service: notify.mobile_app_jkkataja
    data:
      title: Outdoor temperature
      message: Currently - {{ states('sensor.outside') }}
mode: single

Cheers

Looks ok to me. What happens when you use the Developer Tools → States menu to change sensor.outside to be divisible by 5?

EDIT: Oh. Did you create this using the UI Automation editor?

I think it might put extra quotes around the template (giving you a string, not a template). Check what it looks like in the YAML view.

Initially I created it using the UI Automation editor but at this stage I’ve been switching back and forth when trying to get it to work and doing adjustments. The YAML I pasted was taken from UI Automation editor when set to the “Edit as YAML-file” mode.

If I try to remove the quotes when in the “Edit as YAML-file” mode I get the following error (as expected, I assume):
Message malformed: template value should be a string for dictionary value @ data['value_template']

Will try on changing the sensor value from States menu.

Edit: When I set the sensor temperature to 20 or 20.4 manually from Dev Tools → States I got the notification as expected.

Cool. So it is working.

One possible reason it may not work is if the sensor changes from one number divisible by 5 to another all in one go. e.g. 20.x to 15.x. In that case the template stays true. It needs to change from false to true to trigger.

1 Like

Yeah it seems so, I was surprised this morning that I hadn’t gotten any notifications…

…to my embarrassment, upon checking the temperature logs for the night, no 5 degree thresholds had been crossed so no wonder I didn’t get any notifications :man_facepalming:

Well, for me this was a learning experience on to using Dev Tools → States and hopefully someone will find this useful on how to do these kinds of notification automations. Thanks @tom_l !

1 Like

If someone happens to find this thread, here’s also a version of the automation with multiple sensors that works for me. Note that if multiple sensors trigger it at the same time some additional checks might be needed but so far it’s been ok for me.

alias: temperature_5deg_step_notification
description: ""
trigger:
  - platform: template
    value_template: "{{ (states('sensor.outside)|int(0))%5 == 0 }}"
    id: outside
  - platform: template
    value_template: "{{ (states('sensor.storage')|int(0))%5 == 0 }}"
    id: storage
  - platform: template
    value_template: "{{ (states('sensor.livingroom')|int(0))%5 == 0 }}"
    id: livingroom
condition: []
action:
  - service: notify.mobile_app_jkkataja
    data:
      title: |
        {% if trigger.id == "outside" %}
          Outdoor temperature
        {% elif trigger.id == "storage" %}
          Storage temperature
        {% elif trigger.id == "livingroom" %}
          Living room temperature
        {% endif %}
      message: |
        {% if trigger.id == "outside" %}
          Temperature now - {{ states('sensor.outside') }}
        {% elif trigger.id == "storage" %}
          Temperature now - {{ states('sensor.storage') }}
        {% elif trigger.id == "livingroom" %}
          Temperature now - {{ states('sensor.livingroom') }}
        {% endif %}
mode: single

If you’re interested here’s another way to do it. This version will trigger even if the sensor’s value increments by 5 twice in a row.

alias: temperature_5deg_step_notification
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.outside
      - sensor.storage
      - sensor.livingroom
condition: '{{ trigger.to_state.state | int(0) % 5 == 0 }}'
action:
  - service: notify.mobile_app_jkkataja
    data:
      title: '{{ trigger.to_state.object_id | title }} temperature'
      message: 'Temperature now - {{ trigger.to_state.state }}'
mode: single
2 Likes