Set template sensor state in automation

I have an automation that does the following:

  1. Send notification when propane sensor increases by 15% (it is being filled)

  2. Wait 10 minutes

  3. Send notification propane filled to x%. Gallons added (sensor_now - sensor_yesterday)

  4. Set template sensor “propane_tank_last_fill_gal” to state: (sensor_now - sensor_yesterday). This way I can make a lovelace card with gallons filled and date so I can compare when I get my bill

I’m stuck at step 4. Step 1-3 works.

alias: Notification - Propane Fill
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.pro_plus_22d5_tank_percent') | float -
      states('sensor.pro_plus_22d5_tank_percent_yesterday') | float > 15}}
condition: []
action:
  - service: notify.mobile_app_pixel_7
    metadata: {}
    data:
      message: >-
        Propane tank is being filled. Start {{ '%0.1f' |
        format(states('sensor.pro_plus_22d5_tank_percent_yesterday') | float *
        10) }} gallons.
      title: Home Assistant
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: notify.mobile_app_pixel_7
    metadata: {}
    data:
      title: Home Assistant
      message: >-
        Propane filled to {{ '%1.0f' |
        format(states('sensor.pro_plus_22d5_tank_percent') | float) }}%. Gallons
        added {{ '%1.0f' | format(states('sensor.pro_plus_22d5_tank_percent') |
        float * 10 - states('sensor.pro_plus_22d5_tank_percent_yesterday') |
        float * 10) }}.
sensor:
  - name: "propane_tank_last_fill_gal"
    state:
      {% set tank_start = states('sensor.pro_plus_22d5_tank_percent_yesterday') | float * 10 %}
      {% set tank_end = states('sensor.pro_plus_22d5_tank_percent') | float * 10 %}
      {{ tank_end - tank_start }}
mode: single

I get the error Message malformed: extra keys not allowed @ data['sensor']

You can’t configure a sensor inside an automation.

Generally, you do not set the value of sensors, instead you use an appropriate Helper like an Input number and use a service call like input_number.set_value in your automation.

If your goal is to have the sensor’s value only update when this automation runs, you can raise a custom event at the end of the automation and use that event as a trigger for a trigger-based template sensor. The sensor must be configured in your configuration file.

1 Like

Thank you for the advice! I changed the automation step 4 with

  - event: event_propane_fill
    event_data: {}

Then added the following to my template.yaml:

### Update propane_tank_last_fill_gal sensor
- trigger:
  - platform: event
    event_type: event_propane_fill
  sensor:
  - name: "propane_tank_last_fill_gal"
    state: >
      {{ '%1.0f' | format(states('sensor.pro_plus_22d5_tank_percent') |
      float * 10 - states('sensor.pro_plus_22d5_tank_percent_yesterday') |
      float * 10) }}
    unit_of_measurement: "Gal"

In this trigger based template sensor used with platform: event the state for propane_tank_last_fill_gal survived a reboot!

I have a trigger based template sensor with platform: time and the state is lost between reboots, is this normal?

The state of a trigger-based template sensor should survive reboot, assuming one of your triggers isn’t based on homeassistant.restart.