Remembering template sensor state after reboot

Hey,

so I would like to track the odometer reading of my EV since last charge. I’ve created a template sensor:

  template:
    - trigger:
        - platform: state
          entity_id:
            - sensor.citroen_ec4_charging_mode
          from:
            - Quick
            - Slow
          to: No
      sensor:
        - name: "Citroen EC4 Last Charge Finished Mileage"
          state: "{{ states('sensor.citroen_ec4_mileage') }}"
        - name: "Citroen EC4 Last Charge Finished Battery Level"
          state: "{{ states('sensor.citroen_ec4_battery_level') }}"

But afaik it looses value if I restart hass. And that if I understand correctly is expected. Any work around to this?

Another way would be to probably use automation + input_number sensor, and then as automation action use a input_number.set_value service. This way the input number would be persisted across hass reboots?

Can you give more details on this one? Or do you mean that adding event_type: event_template_reloaded is enough?

Yes. Simply using the code posted is enough. Notice @123 has also included if statements in the sensor templates.

Thanks, I was just under impression that I also need to do something not just copy paste :slight_smile:

Anyhow, it does seem that home assistant restarting is not the only issue. Though the sensor.citroen_ec4_charging_mode changed couple of times today, this template stayed in the “unknown” state, so will have to see what is up with that

Check the developer tools state tab - there is a very good chance that the states are actually lowercase - eg:
“quick” “slow” “no”

I deleted my original post because, based on the documentation and tests I have performed, a Trigger-based Template Sensor’s state is restored after a restart.

From Trigger-Based Template Sensor documentation:

The state, including attributes, of trigger-based sensors and binary sensors is restored when Home Assistant is restarted.

Your two Trigger-based Template Sensors simply report the value of two Citroen-based sensors. You should confirm that those two Citroen sensors (mileage/battery_level) report proper values at all times, even after startup.

If your two Trigger-based Template Sensors fail to update at all times then there’s a problem with the State Trigger (follow the advice from mobile.andrew.jones regarding the spelling of the state values).

well this is totally weird. The values are uppercase, no issue here.

I’ve plugged/unplugged the car three times yesterday, and the template sensor stayed in the unknown state.

Tried changing from to not_from, no luck either

          not_from:
            - "unknown"
            - "unavailable"
          to: "No"

Then I decided to add an automation with input number instead.

  automation:
    - alias: Set After charge mileage
      trigger:
        - platform: state
          entity_id:
            - sensor.citroen_ec4_charging_mode
          not_from:
            - "unknown"
            - "unavailable"
          to: "No"
      action:
        service: input_number.set_value
        data:
          entity_id: input_number.citroen_ec4_last_charge_finished_mileage_test
          value: "{{ states('sensor.citroen_ec4_mileage') }}"
  input_number:
    citroen_ec4_last_charge_finished_mileage_test:
      name: Citroen EC4 Last Charge Finished Mileage
      min: 0
      max: 999999
      unit_of_measurement: km

plugged/unplugged, and bam - automation with input_number seemed to work. Thinking that I’m just gonna stick to that went to delete the template sensor, and surprise surprise - that one also had a value!

So probably gonna leave those two for a while to see if they always work :confused:

Thanks you all for help

1 Like