Remembering entity values

Hi,

I was wanting to ask if there was anyway I could transfer a value from my Shelly power monitors to a stored variable of some kind.
The issue is that the power to the Shellys is routinely cut at the end of the pump cycle. When that happens the Energy value becomes “unavailable” which I have defaulted to 0.
This means that I cannot display the value of the Accumulated Energy consumption once the device is off.
I was hoping to transfer the value to a persistant/permanent variable while the device is on. I considered an input_number helper but I cannot assign the entity vaule within the Markdown card.

MARKDOWN CARD OUTPUT (When devices ON)
Device----------------------Now--------------Life time
Ch1 -Main Filter 20 333 kWh
Ch2 In-- Floor 86 211 kWh
Ch3 Filter Pump 11 166 kWh

MARKDOWN CARD OUTPUT (When devices OFF)
Device---------------------Now--------------Life time
Ch1 -Main Filter 0 0 kWh <— I want these LifeTime values to remain when device is off.
Ch2 In-- Floor 0 0 kWh
Ch3 Filter Pump 0 0 kWh

MARKDOWN CARD CODE
‘’’
Ch1 -Main Filter {{ (states(‘sensor.minipm_ch1_mainfilter_power’)|float(0)|round(0)) }} {{ (states(‘sensor.minipm_ch1_mainfilter_energy’)|float(0) | round()) }} kWh
Ch2 In-- Floor {{ (states(‘sensor.in_floor_ch2_power’)|float(0)|round(0)) }} {{ (states(‘sensor.in_floor_ch2_energy’)|float(0) | round()) }} kWh
Ch3 FilterPump {{ (states(‘sensor.shellypmminig3_dcda0db515bc_power’)|float(0)) }} {{ (states(‘sensor.shellypmminig3_dcda0db515bc_energy’)|float(0) | round()) }} kWh
‘’’

## I tried to use this Ch1KWH variable, which does collect the value, but returns to the initial value “99” when the device is off"
‘’’
{% set Ch1KWH = 99 %}
{% if (states(‘sensor.minipm_ch1_mainfilter_energy’)|float(0)) > 0 %}
{% set Ch1KWH = (states(‘sensor.minipm_ch1_mainfilter_energy’)|float(0)) | round(0) %}
{% endif %}
‘’’

Or if I tried to do it outside the markdown card, could I do it in an automation (which runs when the device is on) to store the value? Still not sure where to assign the value to, where it can be accessed later.

Basically I’m looking for a place and way to store an entity value that I can display in a card on the UI.

I’d appreciate any advice or clues.
Mike

Stop doing this:

And feed your energy sensor to a utility meter helper instead:

This will create a sensor that remembers your total when the source sensor is unavailable.

NOTE: really do stop doing this:

Or it will mess your utility meter up with enormous energy spikes when the sensor becomes available again.

Thanks for the advice.
I will implement as you suggest however I’m still interested to know if there is a way to save values somewhere that can be accessed later from other places in HA as a general concept.
Cheers

Sure there are plenty of helpers you can use. In this case, an input_number.

You can only save data in an entity state (or attribute). The only built-in methods for directly assigning a value to an entity’s state or attribute and have that data be persistent (survive restarts) are any of the various input helpers, or else trigger-based template sensors. The latter must be defined in yaml.

Thanks for your thoughts. I considered input_number but noticed that the set_value service (or any service) cannot be called from within the UI card.
I might have a look at the trigger-based template sensor idea. Thanks

It most certainly can. e.g. button card:

type: button
name: Set input number
show_state: false
tap_action:
  action: perform-action
  perform_action: input_number.set_value
  data:
    entity_id: input_number.my_input_number
    value: 5

However you can’t use templates in that. So you call a script that can use templates.

type: button
name: Set input number
show_state: false
tap_action:
  action: perform-action
  perform_action: script.set_my_input_number
script:
  set_my_input_number:
    alias: Set my input number
    sequence:
      - service: input_number.set_value
        target:
          entity_id: input_number.my_input_number
        data:
          value: "{{ states('sensor.minipm_ch1_mainfilter_energy') }}"