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
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 !!!
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 !