Notify on attribute change of several entities

Hi guys,

I want to create my first automation. I read several examples, but there are too many variants. So perhaps if I understand the code for my needs it will clear things. Anyway, here’s what I’m looking for:
I have 4 shelly covers defined. For each entity there’s the attribute has_firmware_update which can be true or false.
So I want to define an automation to notify (notify.notify_hmtl5 for example) when each entity changes from false to true.

Thanks

if the info is in an attribute, you have 2 options:

  1. create a template binary sensor for each entity and extract its attribute, then create an automation that triggers on the template sensor’s state change
  2. create an automation that triggers on time interval (e.g. every 5 min) with a condition that at least 1 of the sensors has attribute to true and then send a notification

I’d personally think the first one feel like more work to do, but it’ll be easier to handle / use, especially for a newb
the 2nd will require less work to set up, but more complex coding, which for a a newb could be a bit overwhelming

so first option:
you create a template binary sensor for each of your shelly covers:

binary_sensor:
  - platform: template
    sensors:
      shelly_cover_1_update_available:
        friendly_name: "Shelly Cover 1 Update Available"
        value_template: >-
          {{ state_attr('binary_sensor.shelly1', 'has_firmware_update') == "true" }}

your automation would look like:

- alias: Notify Shelly Cover Firmware Update
  initial_state: true
  trigger:
    - platform: state
      entity_id: 
        - binary_sensor.shelly_cover_1_update_available
        - binary_sensor.shelly_cover_2_update_available
        - binary_sensor.shelly_cover_3_update_available
        - binary_sensor.shelly_cover_4_update_available
      to: 'on'
  action:
    - service: notify.notify_hmtl5
      data_template:
        message: '{{ trigger.entity_id | replace("binary_sensor.","") | replace("_"," ") | capitalize }}'

Above allows you to have a single automation that gives you the name of the entity that has an update available.

For the 2nd option, well since you’re a newb I would not recommend diving into this just yet, but you can have a look at how I list which windows are forgotten opened when my car gets locked:

Wow! I really appreciate the detailed answer! Thank you!
I think I’ll go for the first option. Seems easy enough.
Couple of questions.

  1. The state platform in trigger cannot work with the original cover entity attribute? Why does it require a separate binary_sensor?
  2. Will it output a single notification per cover or will I get a notification every few minutes until the state changes?

Glad it helped. Feel free to mark as Answer so others can see how you did it.

Triggers only work on state change, as in not on attribute change. You could use the original cover and remove the to: 'on' line, but then you’d also need to add conditions on the templates else it’ll send a notification each time anything changes in that sensor.

one notification per cover when has_firmware_update goes from "false" to "true"

[edit] Correction, you could trigger on an attribute.
Replace the trigger lines from:

    - platform: state
      entity_id: 
        - binary_sensor.shelly_cover_1_update_available
        - binary_sensor.shelly_cover_2_update_available
        - binary_sensor.shelly_cover_3_update_available
        - binary_sensor.shelly_cover_4_update_available
      to: 'on'

to:

    - platform: template
      value_template: "{{ state_attr('binary_sensor.shelly1', 'has_firmware_update') == 'true' }}"
    - platform: template
      value_template: "{{ state_attr('binary_sensor.shelly2', 'has_firmware_update') == 'true' }}"
    - platform: template
      value_template: "{{ state_attr('binary_sensor.shelly3', 'has_firmware_update') == 'true' }}"
    - platform: template
      value_template: "{{ state_attr('binary_sensor.shelly4', 'has_firmware_update') == 'true' }}"

Only thing is the notification will probably not work anymore as the trigger would have changed and not sure trigger.entity_id would work then…

I think this can be simplified a bit with a single automation.

- alias: 
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.shelly1
        - binary_sensor.shelly2
        - binary_sensor.shelly3
        - binary_sensor.shelly4
  condition:
    condition: template
    value_template: >
      {{ trigger.to_state.attributes.has_firmware_update != trigger.from_state.attributes.has_firmware_update and trigger.to_state.attributes.has_firmware_update == True }}
  action:
    - service: notify.notify_hmtl5
      data_template:
        message: '{{ state_attr(trigger.entity_id, 'friendly_name') }} has a firmware update!'
1 Like

One day you should have a look my automations, I bet you could half the number of lines I’m using :slight_smile:

Well, it all would have to be tested. Half the time you remove lines you remove functionality or break functionality. It’s a delicate dance.

Never thought {{"coding" == "dancing"}} would equate to true :smiley:

1 Like