Refactoring 4 similar automations to 1 or 2?

I’ve just added the following 4 automations, would it be possible to refactor these by merging the black + color automations in a single one ?

By monitoring not one but two entities? But I would need to know in the message if it’s the black or colour which is below the threshold…

- alias: Printer ink alert black 50
  trigger:
    platform: numeric_state
    entity_id: sensor.hp_envy_5000_series_black_ink
    below: 50
  action:
  - service: notify.ha_telegram
    data_template:
      message: "Black ink alert : {{ states.sensor.hp_envy_5000_series_black_ink.state }}%"

- alias: Printer ink alert colour 50
  trigger:
    platform: numeric_state
    entity_id: sensor.hp_envy_5000_series_tri_color_ink
    below: 50
  action:
  - service: notify.ha_telegram
    data_template:
      message: "Colour ink alert : {{ states.sensor.hp_envy_5000_series_tri_color_ink.state }}%"

- alias: Printer ink alert black 10
  trigger:
    platform: numeric_state
    entity_id: sensor.hp_envy_5000_series_black_ink
    below: 10
  action:
  - service: notify.ha_telegram
    data_template:
      message: "Black ink alert : {{ states.sensor.hp_envy_5000_series_black_ink.state }}%"

- alias: Printer ink alert colour 10
  trigger:
    platform: numeric_state
    entity_id: sensor.hp_envy_5000_series_tri_color_ink
    below: 10
  action:
  - service: notify.ha_telegram
    data_template:
      message: "Colour ink alert : {{ states.sensor.hp_envy_5000_series_tri_color_ink.state }}%"

Try this:

- alias: Printer ink alert
  trigger:
    - platform: numeric_state
      entity_id: 
        - sensor.hp_envy_5000_series_black_ink
        - sensor.hp_envy_5000_series_tri_color_ink
      below: 10
    - platform: numeric_state
      entity_id: 
        - sensor.hp_envy_5000_series_black_ink
        - sensor.hp_envy_5000_series_tri_color_ink
      below: 50
  action:
    - service: notify.ha_telegram
      data_template:
        message: "{{trigger_to_state.name}} ink alert : {{ trigger_to_state.state }}%"

Wonderful, it seems to be exacly was I was looking for , thanks :grinning:

1 Like