Store calculated values?

Probably a noob question. I have created a number of sensors that are set by automations and calculate a lot of stuff, like energy usage, solar power generated etc. My problem is that the values get ‘lost’ during a restart of HA, wich happens quite a lot since i am always experimenting with it. Can anyone point me in the right direction?

Help would be greatly appreciated!

Take a look at the recorder integration or if you plan on storing the data long term I’d suggest InfluxDB.

1 Like

Hi Burningstone, thanks for the pointer, sounds like a good idea, do the values stored with recorde automatically reload their last known value after a HA restart?

Recorder and influxdb are databases. They store your calculated values (as the title of your topic asks), but they don’t reload their last value on HA restart. For that you can either use the SQL sensor to retrieve them, or more simple, store them in an input_number.

1 Like

btw, it’s a very common expectation here.

1 Like

I recently started monitoring my electricity consumption, and faced a similar problem : having historical data by day of electricity consumed. My ZMAI90 returns total energy used (sensor.mains_consumed_energy). I used the MQTT database to store the calculated values.
Created 2 MQTT sensors :

  - platform: mqtt
    name: "total_energy"
    state_topic: "sensor/totalenergy"    

  - platform: mqtt
    name: "yesterday_energy"
    state_topic: "sensor/yesterday_energy"   

Every day at midnight :

- id: '1569740295999'
  alias: Save consumed energy
  trigger:
  - at: '23:59'
    platform: time
  action:
  - data:
      payload_template: >
        {{ "%.2f"|format( (states("sensor.mains_consumed_energy") | float) - (states("sensor.total_energy") | float))  }}      
      topic: sensor/yesterday_energy
      retain: true
    service: mqtt.publish
  - data:
      payload_template: >
        {{ states("sensor.mains_consumed_energy")}}
      topic: sensor/totalenergy
      retain: true
    service: mqtt.publish
    

Since published with the retain flag, they survive a HA restart.

The sensor.yesterday_energy gets pushed to influxdb to generated nice graphs on daily basis.

1 Like

you publish it to a separate MQTT topic with retain: true, right?
it has some advantages and disadvantages comparing to storing data in input_xxx so in the end of the day it depend on the use case - for example, MQTT allows for as many attributes as you like but you need separate input_xxx to store it, but one have to have MQTT broker up and running and MQTT sensors configured properly.
I know that you know it, just added the note so readers understand their options.

1 Like

Thanks for your input, makes sense, i am trying to get this working, seems to work, another strange thing: one value is shown as a string (‘0.72’) the other value is an actual number, can you define the type of mqtt sensor (numeric of string)?

Screenshot_1

My config:

- platform: mqtt
  name: "solar_power_hour"
  state_topic: "sensor/solarpowerhour"
  unit_of_measurement: kWh
- platform: mqtt
  name: "solar_power_hour_last_val"
  state_topic: "sensor/solarpowerlastval"
  unit_of_measurement: kWh

    - service: mqtt.publish
      data:
        payload_template: >
          '{% if states.sensor.produced_power_hour_last_val.state | float | round(0) == 0 %}
                    {{0 | float}}
                {% else %}
                    {{(states.sensor.shellyem_b9eabf_total_returned_0.state | float - states.sensor.produced_power_hour_last_val.state | float)| round(2)}}
                {% endif %}'
        topic: sensor/solarpowerhour
        retain: true

    - service: mqtt.publish
      data:
        payload_template: >
          {{states.sensor.shellyem_b9eabf_total_returned_0.state | float}}
        topic: sensor/solarpowerlastval
        retain: true
```

Many thanks!

just remove single quotes around tour first payload_template :wink:

1 Like

Hey AhmadK, now i feel stupid :wink:

Many thanks, been staring at it for over an hour…
Happy Easter!

1 Like

in the future paste in into Developer tools - > Templates and you’ll see it

1 Like