Problem using an attribute as value in trigger - float required

I’m new with Home Assistent, so this is probably a trivial problem. But I’ve spent hours on this without success…

I have a sensor providing following data (from developer tools):

Entity: sensor.electricity_price_torngard_201
State:  0.931 
Attributes: 
 - avg_price: 1.313
 - ....

I try using this data in the trigger section of an automation like this:

trigger:
  - type: value
    platform: device
    device_id: 758caeece782c4d1673ef7bfbe883d56
    entity_id: sensor.electricity_price_torngard_201
    domain: sensor
    below: '{{ states.sensor.electricity_price_torngard_201.attributes.avg_price }}'

but I get the following error in the log:

"Invalid config for [automation]: expected float for dictionary value @ data[‘below’]. Got None. "

I have tried to pipe data through float, without result. Replacing the attribute with a value works fine. Like this:
below: 2.0

Eventually I would like to do some calculation and multiply the attribute value with a percentage value, but first of all I need to get the basic stuff working…

You can’t use templates in below: in numeric state triggers, so I’m guessing that goes for device triggers too (don’t use device automations by the way, they will make your life difficult, not easier).

Try this instead:

trigger:
  - platform: template
    value_template: "{{ states('sensor.electricity_price_torngard_201')|float(0) < state_attr('sensor.electricity_price_torngard_201', 'avg_price')|float(0) }}"

What do you mean by that?

  1. They don’t support templates.
  2. They rely on the entity’s device_id which, under certain circumstances, could change thereby rendering the Device Trigger inoperative.
  3. They’re more verbose than other triggers (State, Numeric State, etc) which increases the automation’s length.
2 Likes

Thank You, so to be clear, we’re talking about using device as triggers?

Thanks, you made my day (hopefully…)! This was not easy to figure out for a newbie like me. Now, I don’t get any error logs any more at least. I need to wait a while before I can confirm the trigger works as espected as well. As far as I understand, the expression in the value_template will be evaluated every time any of the involved states are changed, and if the condition is true, the action will be executed.

Well, syntax OK but does not trigger on state changes:

id: '1646920153984'
  alias: Daikin normal
  description: 'Will restore setpoint to standard temperature'
  trigger:
  - platform: template
    value_template: "{{ states('sensor.electricity_price_torngard_201')|float(0) < state_attr('sensor.electricity_price_torngard_201', 'avg_price')|float(0) * (1.0 + states('input_number.indata')/100 ) }}"
  condition: []
  action:
  - service: climate.set_temperature
    target:
      entity_id: climate.gamla_huset
    data:
      temperature: '{{ states.climate.gamla_huset.attributes.temperature | float  +
        2.0 }}'
      hvac_mode: heat
  mode: single

So my intention is the setpoint should be increased by 2 degrees (restored to normal temp) when price is lower then a treshold value based on average price increased by X%. The action works fine, but the automation does not trigger. I expect it should trigger by any change in electricity price, average or the percentage value (input_number). Bothing happens when I change the percentage value. So I’m still lost unfortunately.

Finally I came up with a solution. I used a basic trigger for any change of price or percentage value. Then I added the condition with a template testing if price is higher/lower than the treshold.

  trigger:
    - platform: state
      entity_id: 
        - sensor.electricity_price_torngard_201
        - input_number.indata
      to:
  condition:
      condition: template
      value_template: "{{ states('sensor.electricity_price_torngard_201')|float(0) < state_attr('sensor.electricity_price_torngard_201', 'avg_price')|float(0) * (1.0 + (states('input_number.indata')|float(0))/100.0 ) }}"
  action:
    - service: climate.set_temperature
      target:
        entity_id: climate.gamla_huset
      data:
        temperature:
          "{{ states.climate.gamla_huset.attributes.temperature | float  +
          2.0 }}"
        hvac_mode: heat
  mode: single

Now I have to add an binary_sensor to keep track of if thermostate setpoint is reduced or not, to prevent multiple increase/decrease of setpoint.