Create histogram in automation

Hi all,
I’m using Home Assistant, on a personnal server, without Hassio.

I’m not sure to be on the right way, so, I will explain my situation and goals first before talking about histogram.
I’m playing with Pzem004t-v3 and esp12F to measure power consumption of some element in my house (water heater, watrer pump and so on).
Integration is working fine, I can get voltage, current, power and energy.

I created sensors in configuration.yaml that give me daily and monthly consumption peaks and offpeaks period.
Each starting days, those values are reset and lost. Now, I would like to savec them into news sensors, in order to get a kind of histogram.

I created a new sensor in configuration.yaml :

sensor:
  - platform: template
    sensors:
       histo_cumulus:
        friendly_name: "histo cumulus"
        unit_of_measurement: '€'
        value_template: "states('sensor.histo_cumulus')"

And now, I would to create an automation which will save the daily value in this new sensor:

- id: '1594636237848'
  alias: save-histo-cmulus
  description: ''
  trigger:
  - at: '23:59:59'
    platform: time
  condition: []
  action:
  - entity_id: sensor.histo_cumulus
    state: sensor.facture_cumulus

(cumulus is a water heater in french).

I have two questions :slight_smile:
1- Am I on the right way?
2- if yes, how can I change histo cumulus value in an automation action
I know that state doesn’t exist, It is just an example of what I want to do)

No not by the looks of it.

This sensor is referencing itself:

sensor:
  - platform: template
    sensors:
       histo_cumulus:
        friendly_name: "histo cumulus"
        unit_of_measurement: '€'
        value_template: "states('sensor.histo_cumulus')"

And there is no service defined in your automation actions:

  action:
  - entity_id: sensor.histo_cumulus
    state: sensor.facture_cumulus

What you need is something like this (read the comment in the code):

sensor:
  - platform: template
    sensors:
       histo_cumulus:
        friendly_name: "histo cumulus"
        unit_of_measurement: '€'
        value_template: "states('sensor.facture_cumulus')"
        update_interval: 9999999999 # 317 years. Essentially it will never update 

So we update it manually with the automation:

  action:
    service: homeassistant.update_entity
    data:
      entity_id: sensor.histo_cumulus

Unfortunately this may not work as a template sensor is a local push sensor not a polling sensor so the scan interval trick may not work. Try it and see. If it does not work I have another way to do it.

Hi tom_l,

Thanks for your answer. I will try this, and give here updates!

EDIT : I got this error:

Invalid config for [sensor.template]: [update_interval] is an invalid option for [sensor.template]. Check: sensor.template->sensors->histo_cumulus->update_interval. (See ?, line ?

and effectively, there is no update_interval option in https://www.home-assistant.io/integrations/template/

So, there is no way to “save” some value in automation?

I had a brain fart. It’s called scan_interval. Not update_interval. See:

Maybe I miss something, but the configuration is stil invalid.
Invalid config for [sensor.template]: [scan_interval] is an invalid option for [sensor.template]. Check: sensor.template->sensors->histo_cumulus->scan_interval. (See ?, line ?).

It seems that scan_interval is only available for platform, not for sensors.

Here is my conf in configuration.yaml.


sensor:
  - platform: template
    sensors:
      histo_cumulus:
        friendly_name: "histo cumulus"
        unit_of_measurement: '€'
        value_template: "states('sensor.facture_cumulus')"
        scan_interval: 9999999999 # 317 years. Essentially it will never update