Trigger on a type of device class when any of them changes(not necessarily binary), group not preferred as future devices added may not be automatically added

I’m trying to build an automation that will trigger for any moisture sensor, I got as far as following:

{{ states.binary_sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', 'eq', 'moisture') | selectattr('state', 'eq', 'on') | map(attribute = 'name') | list | length > 0 }}

Currently I see 2 problems here:

  1. I have to know the desired state of the target sensor and they must be all the same, for example if I target temperature sensor I cannot trigger when any of them report a change in temperature and then further decide followup actions based on areas they are in.
  2. The trigger will only work when the first sensor goes into desired state, because if a second sensor goes into descired state, the template will still return true and the trigger won’t fire again.

I wonder how can I workaround these limitations? Suggestions appreciated.

If you need to know which one of the moisture sensors turned on, you’ll need to list them individually in a State Trigger. There’s no simple workaround in this situation.

If you don’t need to know which one turned on, just that one of them turned on, create a Template Sensor that reports the number of moisture sensors that are currently on (your existing template already does that, just remove the final 'greater than zero '). Use it in a State Trigger to detect whenever the Template Sensor’s value changes.

1 Like

Cool, let me try it.
So the template trigger can work with value returned other than true/false, good to know.

I think I can also rely on this.trigger to figure out which sensor triggered the change.

=== Update ===
Unfortunately it didn’t work for me, it will only trigger when the return value change from 0 to a non 0 value.

It seemed the official doc says non-zero number are treated equally as true.
Automation Trigger - Home Assistant(-,a%20non%2Dzero%20number,-or%20any%20of

If you create a Template Sensor that reports the number of moisture sensors that are currently on (sensor.moisture_quantity), it can be used in a State Trigger. However, like I mentioned in my previous post, you won’t be able to easily know which one just turned on (when more than one moisture sensor is on).

alias: example
description: Notify whenever another moisture sensor turns on
trigger:
  - platform: state
    entity_id: sensor.moisture_quantity
    to:
condition: 
  - "{{ trigger.to_state.state | int(0) > trigger.from_state.state | int(0) }}"
action:
  - service: notify.persistent_notification
    data:
      message: >
        There are {{ trigger.to_state.state }} moisture sensors that are on.

It sounds like an overlook in design, I wonder if state trigger accept value_template attribute instead of entity_id?

It does not.

Reference: State Trigger

If neither of my two suggestions meets your needs, you’ll have to compromise on your requirements (or, if you are a software developer familiar with python, submit a PR to the Core repository that modifies the State Trigger’s operation).

Sounds good :+1:
I’ll look into setup dev and build environment next.

1 Like