When you use platform:template twice, the second one overwrites the first. Put both into the same platform. That’s why the section is called ‘sensors’ and not ‘sensor’.
Also, to be safe, you should probably swap to states_attr() so the value has a default when it cannot communicate to the device, take a look at the templating page:
Do I understand you correctly. Two platform: template entries, one defining sensor A, and one defining sensor B, will result in only one entity (because the second overlays the first). But, one platform: template entry that includes two sensors will result in two entities?
Yes, I’m not sure which one will override, the first or second. One will override the others. But treat the whole section as a container for all your template sensors. I have like 14 in mine:
- platform: template
sensors:
main_door_hindge: # MAIN DOOR SENSOR #
value_template: >
{% if is_state('sensor.main_door_t_access_control_9_9', '23') %}
closed
{% elif is_state('sensor.main_door_t_access_control_9_9', '22') %}
open
{% else %}
closed
{% endif %}
friendly_name: Main Door Status
main_door_battery: # MAIN DOOR BATTERY #
value_template: >
{% if states('sensor.main_door_t_access_control_9_9') in ['22','23'] %}
{{ states.sensor.main_door_t_access_control_9_9.attributes.battery_level }}
{% else %}
100
{% endif %}
friendly_name: Main Door Battery
unit_of_measurement: "%"
entry_door_hindge: # GARAGE ENTRY DOOR SENSOR #
value_template: >
{% if is_state('sensor.entry_door_t_access_control_18_9', '23') %}
closed
{% elif is_state('sensor.entry_door_t_access_control_18_9', '22') %}
open
{% else %}
closed
{% endif %}
friendly_name: Garage Entry Door Status
entry_door_battery: # GARAGE ENTRY DOOR BATTERY #
value_template: >
{% if states('sensor.entry_door_t_access_control_18_9') in ['22','23'] %}
{{ states.sensor.entry_door_t_access_control_18_9.attributes.battery_level }}
{% else %}
100
{% endif %}
friendly_name: Garage Entry Door Battery
unit_of_measurement: "%"
hallway_motion_battery:
value_template: >
{% if states('states.sensor.hallway_ms_burglar_32_10') in ['0','3','8'] %}
{{ states.sensor.hallway_ms_temperature_32_1.attributes.battery_level }}
{% else %}
100
{% endif %}
friendly_name: Motion Sensor Battery
unit_of_measurement: "%"
harmony_activity:
value_template: >
{% if is_state("remote.living_room", 'on') %}
{{ states.remote.living_room.attributes.current_activity }}
{% else %}
PowerOff
{% endif %}