Template sensors do not list correctly

Hello!

I got an issue since I added an energy meter (Zigbee) on my HA.

I added the templated sensor like this:

    - name: lixee_zlinky_tic_metering_HC
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: total_increasing

right after my other templated sensor:

template:
  - sensor:
    - name: "Ventillation Grande Vitesse"
      state: >
        {% if is_state('light.ventilation_highspeed', 'on') %}
          1
        {% else %}
          0
        {% endif %}

    - name: lixee_zlinky_tic_metering_HC
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: total_increasing

But when I reload the configuration, I lose my “ventillation grande vitesse”: “Entity not available”.
I noticed that when I add a state line:

    - name: lixee_zlinky_tic_metering_HC
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: total_increasing
      state: 37107762

everything loads as expected.

The point is that, setting an initial state is a big deal: when the HA restarts or fails, the energy panel loads up the sensor and compare with the state value and I end up with erratic consuptions! Actually, after three month of using the device, I had te restart today HA and now today consuption is 21kW (i.e. the sum of the last three months…) which lead to pricing miscomputation.

Anyone could help on this figuring why I have to set a state entry and what to set for it to work well?

Regards,
Paul

That sensor does not have a state template. That is not optional.

Thanks for the reply.

How can I solve this issue? The goal is to let HA uses the latest know value instead a fixed one that goes more and more obsolete as the time passes by.

Latest known value of which entity?

I guess I’m missing something if it is not obvious :slight_smile:

I got a Lixee device that sends me ernergy data. This is to get that device/sensor displaying its data (in the energy panel) that I wrote that templated sensor.
Is there another way to do this?

When reading the doc about ernegy/total_increasing sensors, I see that they are expected to start at 0 or to considere the first data received as the first state for later computation (increase). But here, I have to set the initial value, which is conceptually inconsistent and actually leads to incorrect data as soon as the device disconnects/reconnects.

What should be the correct way of declaring such a device to get it used?

Best regards,

That template sensor has no state template it is not referencing any device or entity.

Also once you do add a state template it will only update when the source sensor updates.

You make me realized I’m lost :smiley:

I got back my actual device/entity. With the values I want. Now, I’m trying to figure out how I did linked the entity to the template sensor. I don’t even understand why I did a templated sensor :sweat_smile:
I surely followed a tutorial at one point without understanding what I was doing (or where :wink: )

Soooo. Many thanks for your help as you allowed me to understand I was wrongly manipulating templates instead of actual entities! (And that I’m not really good at playing with HA!)

Best regards,
Paul

Well, I managed to understand what I made.

I have the device with several entities


But the “Tier Summation” does not goes into the Energy Panel. Then, I set template sensor of device class Energy to get them displayed.

To get it updated, I wrote an automation to feed the templated sensor:

alias: Linky Update
description: Mise à jour des données Linky
trigger:
  - platform: time_pattern
    hours: "*"
    minutes: /1
    seconds: "0"
condition: []
action:
  - service: zha_toolkit.execute
    data:
      command: attr_read
      ieee: 00:15:8d:00:05:d2:6a:c9
      cluster: 1794
      attribute: 256
      state_id: sensor.lixee_zlinky_tic_metering_HC
      allow_create: false
  - service: zha_toolkit.execute
    data:
      command: attr_read
      ieee: 00:15:8d:00:05:d2:6a:c9
      cluster: 1794
      attribute: 258
      state_id: sensor.lixee_zlinky_tic_metering_HP
      allow_create: false
  - service: zha_toolkit.execute
    data:
      command: attr_read
      ieee: 00:15:8d:00:05:d2:6a:c9
      cluster: 1794
      attribute: 32
      state_id: sensor.lixee_zlinky_tic_current_contract
      allow_create: false
  - service: zha_toolkit.execute
    data:
      command: attr_read
      ieee: 00:15:8d:00:05:d2:6a:c9
      cluster: 1
      attribute: 0
      allow_create: false
      state_id: lixee_zlinky_tic_mains_voltage
mode: single

All that makes my system working: my data is well read and transfered to the Energy panel.

The point is that when an error occurres, or I have to reset HA, The templates are reset and take the default values. which makes the energy data to explode:


(“normal day”:slight_smile:

Do you have an idea how get ride of the inconsistent value attribute? Maybe replacing “allow_create: false” attribute to the zha_toolkit service data…

I see that a lot of users are making use of Zigbee2MQTT which seems to do the trick. But I don’t really want to change all my entites… (and for some reason, Z2M does not start…)

Regards,

Note that is the tutorial I used, the template is using “state: unavailable” instead of an actual numeric value. I did not do that beacause I got failures… Not sure why.

I will try with that.

Thanks for reading.

Hello!

I eventually deal with my issue. A few weeks ago. I take time to write here my solution :

I set a storage entry to remember the current value. Then I changed the default value to read from that entry. I works fine from then. I’m not happy with the complexity, but…

    - name: lixee_zlinky_tic_metering_HP
      unit_of_measurement: "Wh"
      device_class: energy
      state_class: total_increasing
      #state: 42460988
      #state: unavailable
      state: >
        {{ states("input_number.linky_last_hp") | int }}

Regards.