Retrieve data from a trigger in an automation

I’m pretty new to home assistant so I might be doing something really obvious wrong. But I can’t seem to get it working.

I want to send the actual state of a trigger in my automation to a rest api. I have the following automation set up:

alias: Send current power consumption
description: ""
trigger:
  - type: power
    platform: device
    device_id: 12b09f34b1e61020dd7d86cc5c2d2d7b
    entity_id: sensor.electricity_meter_power_consumption
    domain: sensor
    above: 0
condition: []
action:
  - service: rest_command.electricity_consumption
    data:
      CONSUMPTION_AMOUNT: states('sensor.electricity_meter_power_consumption')
mode: single

I’d like to get the actual value of the sensor.electricity_meter_power_consumption entity.
I’ve tried:

  • state_attr(‘sensor.electricity_meter_power_consumption’)
  • sensor.electricity_meter_power_consumption
  • trigger.event.data.sensor.electricity_meter_power_consumption

But they all don’t give the expected result.
How can I fix this?

Templating

Trigger variables

alias: Send current power consumption
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.electricity_meter_power_consumption
    above: 0
condition: []
action:
  - service: rest_command.electricity_consumption
    data:
      CONSUMPTION_AMOUNT: "{{ trigger.to_state.state }}"
mode: single

EDIT: Changed trigger type to Numeric State

Great that got me a little further!

But now when the automation is triggered I get this error:

Error rendering data template: Result is not a Dictionary

What does this mean and how do I fix it?

I had meant to change it to a a Numeric state trigger, instead of a Device trigger (I have edited the automation above).

Double check that your data variable is correct… while possible, it is rare to have variables with capital letters.

Also, be aware that by defining an above in your trigger your actions will only execute when the triggering sensor goes from 0 to above zero… it will not continue to update whatever the endpoint of your REST command is as it increases beyond that point.

Yes that did the trick!!

Thanks a lot for your help Drew!