Prevent Light Template to get temporary NULL state when reloading all Templates

Hello !

In order to optimize my database usage, I’ve defined many templates for entities that I want to record. Let’s take an example :

This a light entity created by Zigbee2MQTT :

light.ampoule_e27_salon_coin_full :

Attributes:

effect_list: blink, breathe, okay, channel_change, finish_effect, stop_effect
supported_color_modes: brightness
color_mode: brightness
brightness: 203
last_seen: 2021-08-24T19:10:41.750Z
linkquality: 51
power_on_behavior: previous
update: 
state: idle
update_available: false
friendly_name: Ampoule_E27_Salon_Coin_full
supported_features: 45

So many data I don’t care about : effect_list, last_seen, linkquality, power_on_behavior, update, …
Plus the way the recorder works : on each linkquality change or even each last_seen change, everything is recorded. Which is crazy, but OK. Deal With it.

So for example I’ve created this light template :slight_smile:

      e27_salon_coin_mini:
        availability_template: "{{ states('light.ampoule_e27_salon_coin_full') not in ['unknown','unavailable','None'] }}"
        value_template: "{% if is_state('light.ampoule_e27_salon_coin_full', 'on') %} on {% else %} off {% endif %}"
        level_template: "{% if is_state('light.ampoule_e27_salon_coin_full', 'on') %} {{state_attr('light.ampoule_e27_salon_coin_full', 'brightness')}} {% endif %}"
        turn_on:
          service: light.turn_on
          target:
            entity_id: light.ampoule_e27_salon_coin_full
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.ampoule_e27_salon_coin_full
        set_level:
          service: light.turn_on
          target:
            entity_id: light.ampoule_e27_salon_coin_full
          data:
            brightness: "{{ brightness }}"

Which gives me this new entity : light.e27_salon_coin_mini

Attributes :

supported_color_modes: brightness
color_mode: brightness
brightness: 201
friendly_name: e27_salon_coin_mini
supported_features: 1

It works perfectly. It is very light and only add a new small record in database when light state changes or when brightness changes ! Perfect !!! :heart_eyes:

Actually it works almost perfectly… I have a trouble and I can’t find a solution. I’ve tested many things.
The issue is : whenever I reload Templates from Configuration menu, my template gets a very temporary ‘NULL’ state. It lasts a few milliseconds but 2 records are created in database, one with NULL state and a few milliseconds later with the actual state.

My question is : how can I prevent this simple template to get a NULL state when reloading all templates ?

Thank you ! :slight_smile:

Grrrr, I can’t find a way to do that. I’m pretty sure there should be something easy to add in the valeu_template to prevent to get a NULL value but I can’t find it… :confused:

Still did not find any solution on my own… last bump to see if anyone has a clever idea ! :wink:
If not, I will live with it I guess.

1 Like

there is no solution. You’re reloading the templates which initializes the entities being reloaded. When this happens, they start with a default state of None which translates to unkonwn. Why are you reloading so much? You should only have to reload when you’re creating entities, which will be rare in a mature HA setup.

1 Like

Thanks.
You’re absolutely right. Wouldn’t be a problem in a stable/mature setup.
The problem is that I’m far from here. I keep adding things almost every weeks, and when I don’t add physical devices, I customize my entities, my automotions, having new ‘needs’,…

I will live with it.
I’ve decided to build my own ‘presence simulation’ automation on Node-Red using history. I will just have to ignore those ‘NULL’ states, no problem.

The advantage is that I have divided by more than 10 the number and global size of records for each of my lights. Pretty happy with it. :slight_smile:

Thanks for you help Petro, very appreciated, on this topic and so many others.