#Numeric_state Trigger with Multiple Entities, and value_templates

1 messages · Page 1 of 1 (latest)

vagrant thorn
#

Does value_template: only work for a single entity as shown in the Automations docs?
I need a value_template: on some, but not all entities. I can't get this to work:

trigger:
  platform: numeric_state
  entity_id:
    - sensor.den_hallway_4in1_battery_level  # Reported in percent
    - sensor.water_leak_sensor_dishwasher_battery_level_2 # Reported in percent
    - sensor.mbr_ws3910b_wh68_battery # Reported in Volts. Convert to percent
      value_template "{{ 100.0 * state.state | float(0.0) / 3  }}"
    - sensor.mbr_ws3910b_wh40_battery # Reported in Volts. Convert to percent
      value_template "{{ 100.0 * state.state | float(0.0) / 3  }}"
  below: 1
weak harness
#

The variable ‘state’ is the state object of the entity specified by entity_id.

#

Presumably you can make decisions in the template based on the entity that changed

vagrant thorn
#

I'm getting a parsing error:

#
Error loading /config/configuration.yaml: while parsing a block collection
  in "/config/automations.yaml", line 1686, column 7
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 1695, column 9

and have specified `states['sensorname'] instead of state.state

#

I can't figure out the syntax to use value templates against multiple entities, or for some of them and not others

#

if its supported

#

It looks like the value_template is at the entity_id level and applies to all entities. Oh well.

weak harness
#

You can only have one value_template

#

You need to make a decision in the template about what to do

vagrant thorn
#

👍

weak harness
#

You have the state object that changed, so adjust it it or don't adjust it based on the entity_id

vagrant thorn
#

I'm thinking something like this but I'm not sure if I have the reference to the trigger's attribute quite right. Searching

    value_template: >-
      {% if ( state.attributes.unit_of_measurement == '%' ) %}
        {{ state.state }}
      {% else %}
        {{ 100.0 * (state.state | float(0.0)) / 3  }}
      {% endif %}
weak harness
vagrant thorn
#

trigger.to_state.entity_id ?

weak harness
#

No, there's no trigger available there

#

You have the state object

#

You can check the attributes directly

#

state.attributes.unit_of_measurement == '%'

vagrant thorn
#

OK, so state.xxx will always be for the entity that triggered. Thank you

weak harness
#

Yes

vagrant thorn
#

Thanks again. I changed it in the example just above

weak harness
#

Syntax is still off

#

Missing %}

#

And {% endif %}