I have a Tasmotized iFan02 controller that I’m trying to figure out a state template for.
the state topic for the fan is sent for both a change to the state of the light and the fan but contain different payloads.
The topic is: “stat/sonoff_MBR_fan/RESULT”
When the light is turned on the MQTT message is “{ “POWER1” : “ON” }”
When the fan speed is changed, to low for example, the MQTT message on the same topic is: “{ “FanSpeed” : 1 }”
the state template for the fan looks for the FanSpeed payload information but if I switch the light that info doesn’t exist in the message and I get an error.
Here is something similar to what I’m trying to accomplish but I just can’t find the right syntax to use to test the MQTT message data for the existence of “FanSpeed”:
state_value_template: >
{% if value_json.FanSpeed is defined %}
"{% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}"
{% endif %}
the non-commented code works but the commented code doesn’t
Basically what I want to have happen is that if the status MQTT message doesn’t contain the “FanSpeed” information I want the “state_value_template” to remain unchanged from the existing value.
I think you need an else statement for the check that retrieves the existing state of the entity. Something like
{% if value_json.FanSpeed %}
# work out value from FanSpeed
{% else %}
# Get existing state of sensor
{{ states('fan.master_bedroom_fan') }}
{% endif %}
Sorry for the late response. My connection to the forum was veryy spotty yesterday. Anyway…
I already had tried that in my many other failed attempts so I thought that it was incorrect. However, I tried it again in the template editor and everything seemed to work.
HOWEVER, when i copied over to the yaml and restart it doesn’t work.
Here is the template:
{% set value_json={"FanSpeed":1} %}
{% if value_json.FanSpeed is defined %}
"5"
{% else %}
"{% if states.fan.master_bedroom_fan.state == 'off' -%}0{%- elif states.fan.master_bedroom_fan.state == 'on' -%}4{%- endif %}"
{% endif %}
If I put a value of 0 then I get a response of “5”. If I change the key to “Fanpeed” (no S) then I get “0”. If I switch around the “on” & “off” in the second line I get “4”. So that all seems to work perfectly.
Here is the yaml:
state_value_template: >
{% if value_json.FanSpeed is defined %}
"{% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}"
{% else %}
"{% if states.fan.master_bedroom_fan.state == 'off' -%}0{%- elif states.fan.master_bedroom_fan.state == 'on' -%}4{%- endif %}"
{% endif %}
No matter what the value of the “FanSpeed” that gets returned from the MQTT then the test always returns a “0”.
It still looks like the “… is defined” statement is always being evaluated to ‘false’ so it always looks at the “else” but since that is already ‘off’ then the template just always returns ‘off’.
I’ll do some more testing but I think the "'s are there to turn the result into a string as required by the component for the state. It turns the returned data from 4 to “4”. I’ll try it without them when I get a chance and see what happens.
I think the -% character just forces a elimination of whitespace between the values. I don’t know if it’s necessary but I don’t think it will hurt anything.
On your testing did your state of the entity show that it was “off” or “on” reflecting the actual state of the fan?
My fan actually turned on to the correct speed correctly and if you look at the attributes the speed is updating correctly but the state always shows “off” even though the fan is actually running in, for example, medium speed.
no matter what I do using the “if…” statements the state always shows as “off” no matter what the actual real world state is. If I just use the plain template without the “if…” part both the state and speed both work correctly.
In that case, I think you need to surround the values returned with quotes, rather than the whole line. I suspect, from experience with other languages, that surrounding the whole line with quotes will cause the interpreter to treat the whole line as a string, rather than a command. I didn’t use any quotes in my testing.
I don’t have a fan. I just sent an mqtt message that turned the state of the entity from on to off and back again.
“3. You must surround single-line templates with double quotes ( " ) or single quotes ( ' ).”
Since the quotes are required for single line templates if the same line is put in a multi-line template and surrounded by quotes it apparently acts completely differently.
When the interpreter encounters the quote mark in the single line context, it is still interpreting yaml syntax, and the quotes are needed to surround all the jinja syntax.
However, in the longer case, it is already interpreting jinja syntax, so the quotes are interpreted as surrounding a jinja text string, rather than jinja commands.
Such complications are a major reason why I use appdaemon rather than HA automations.