I have two MQTT sensors that I’m pulling into a template sensor to calculate a combined value, i.e. when sensor A = 1 and sensor B = 0 then display Auto…etc
Below is the logic for my two sensors plus the third templated sensor
- platform: mqtt
name: "Generator Auto Start/Stop"
unique_id: generator_auto_start_stop
scan_interval: 1
state_topic: "N/b827eb57c4de/settings/0/Settings/Generator0/AutoStartEnabled"
value_template: '{{value_json.value}}'
- platform: mqtt
unique_id: "generator_manual_start"
name: "Generator Manual Start"
scan_interval: 1
state_topic: "N/b827eb57c4de/generator/0/Generator0/ManualStart"
value_template: '{{value_json.value}}'
# Third Sensor where I combine the top two sensors:
- platform: template
sensors:
generator_state:
entity_id:
- sensor.generator_auto_start_stop
- sensor.generator_manual_start
value_template: >
{% if states.sensor.generator_auto_start_stop|string == '1' and states.sensor.generator_manual_start|string == '0' %}
Auto Start/Stop
{% elif states.sensor.generator_auto_start_stop == '0' and states.sensor.generator_manual_start == '0' %}
Off
{% elif states.sensor.generator_auto_start_stop == '0' and states.sensor.generator_manual_start == '1' %}
On
{% else %}
Else: {{ states('sensor.generator_auto_start_stop') }} {{ states('sensor.generator_manual_start') }}
{% endif %}
The result of the last if statement is it defaults to else where I have it print the values which end up being Else: 1 0 - Given those values it should have matched the first if statement.
I validated the type of sensor.generator_manual_start by writing a value_template of:
{% if states('sensor.generator_auto_start_stop') is string %}
string
{% elif states('sensor.generator_auto_start_stop') is float %}
float
{% elif states('sensor.generator_auto_start_stop') is number %}
number
{% elif states('sensor.generator_auto_start_stop') is integer %}
integer
{% else %}
no fucking clue
{% endif %}
and it returned string
I am pulling my hair out trying to do what I thought was a simple sensor that made a result from two other sensors. What am I missing?
Thank you, I’m still very new to HA and I didn’t realize i needed to use a function call states('...'). I also realized casting the type as a int was putting it to 0 even though there wasn’t a valid value, so that was confusing me.
Below is my current version, any feedback appreciated
- platform: template
sensors:
generator_start_stop:
friendly_name: Generate Start/Stop State
value_template: >
{% if states.sensor.generator_auto_start_stop and states.sensor.generator_manual_start %}
{% if is_state('sensor.generator_auto_start_stop', '1') and is_state('sensor.generator_manual_start', '0') %}
Auto Start/Stop
{% elif is_state('sensor.generator_auto_start_stop', '0') and is_state('sensor.generator_manual_start', '0') %}
Off
{% elif is_state('sensor.generator_auto_start_stop', '0') and is_state('sensor.generator_manual_start', '1') %}
On
{% elif is_state('sensor.generator_auto_start_stop', '1') and is_state('sensor.generator_manual_start', '1') %}
On
{% else %}
N/A
{% endif %}
{% else %}
N/A
{% endif %}
clever, I do like the simplicity of {% if states.sensor.generator_auto_start_stop and states.sensor.generator_manual_start %} - I didn’t realize that it would evaluate as true/false if there’s a value or not.
The other section is clever but I feel like I’d need to leave comments to explain what the different values map to, i.e. auto=1 and manual = 0: Auto Start/Stop…
To be fair, that first line is taken directly from your example. If sensor.generator_manual_start doesn’t exist, then states.sensor.generator_manual_start evaluates to ‘None’ which is handled like False by the if statement.
The dictionary called ‘map’ is self-documenting.
The ‘trick’ here is it leverages the fact there are two sensors whose states are either 0 or 1. That represents 22 combinations which can be represented as binary numbers 00, 01, 10, 11 (decimal 0, 1, 2, 3).