Summary: I’m trying to populate the value of a sensor template at HA startup to reflect a state based on two real sensors from a Shelly Uni device (built-in Shelly integration).
HA: 2022.5.4
Background:
I’ve created a garage door template which otherwise works well, similar to what’s referenced here: Garage door cover with two reed sensors - template question - #3 by kirijanker
Mine is created with a Shelly Uni device, which provides the switch entity and two sensor entities, one for closed state and one for open state (using magnetic reed switches).
The sensor template for the cover template provides four states based on the two device’s real sensors: open, closed, opening or closing. It works very well while HA is running, as the template sensor is updated any time the state of the real sensor changes.
Issue #1 - Trigger only on device/external sensor state changes:
When HA starts, the value of the template sensor may be incorrect if the garage door’s state has changed while HA was down. This is how other examples elsewhere are written and I haven’t noticed any with a solution.
The triggers within the template that allow setting its state during normal operation aren’t activated at startup. So the conditional expressions for the template which set the state values are never evaluated and the state is loaded from a HA cache which reflects the value before restart/shutdown
Issue #2 - Adding a trigger for Home Assistant Start:
If a homeassistant start trigger is added, the template sensor will be marked “unavailable” after startup/restart. It becomes available again with the correct state after the real sensors values are changed. This is because an error is caused evaluating to_state condition which is invalid as HA first starts up.
Required
The sensor template requires the ability to show 4 states during normal operation, Open, Closed, Opening, Closing (4 conditions). When HA starts, the real initial state of the door needs to be populated, which is either Open or Closed (2 conditions).
Code from configuration.yaml (shown with the HA start event trigger and sensor triggers):
# Garage Door Template from Shelly Uni Switch and Sensors
cover:
- platform: template
covers:
garage_door:
device_class: garage
friendly_name: "Garage Door"
unique_id: "shellyGarageDoor"
value_template: '{{ states("sensor.garagedoor_status") }}'
open_cover:
- service: switch.turn_on
target:
entity_id: switch.garage_door_button
close_cover:
- service: switch.turn_on
target:
entity_id: switch.garage_door_button
stop_cover:
- service: switch.turn_on
target:
entity_id: switch.garage_door_button
icon_template: >-
{% if is_state('sensor.garagedoor_status', 'closed') %}
mdi:garage
{% elif is_state('sensor.garagedoor_status', 'closing') %}
mdi:garage-alert
{% elif is_state('sensor.garagedoor_status', 'opening') %}
mdi:garage-alert
{% else %}
mdi:garage-open
{% endif %}
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.garage_door_open
- binary_sensor.garage_door_closed
- platform: homeassistant
event: start
sensor:
- name: "GarageDoor_Status"
unique_id: "shellyGarageDoor_Status"
state: >
{% if is_state('binary_sensor.garage_door_closed', 'on') %}
closed
{% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_open') %}
closing
{% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_closed') %}
opening
{% else %}
open
{% endif %}