I have the following config of an input selector, a mqtt position sensor and a mqtt switch. It a controller for my rollershutters - 100% are closed and 0% are opened:
and youâll get unknown because there is no such entity.
then add int filter
{{ states('input_select.rollershutter_badog.state') | int }}
and youâll get 0 because itâs a default when conversion fails.
so you and up with 0 < 100, which is always True so your A and 0<100 always depends on A only.
Which version are you testing? Your original one or the one AhmadK suggested?
I suggest you use AhmadKâs version and, as a test, manually trigger it. Go to Developer Tools > Services and complete the form like in the image below then click Call Service.
When I call the service, an entry in the logbook is created.
But I see no mqtt messages on the topics.
How can I provide a preset value when I call the service?
AhmadKâs example will, at minimum, publish to the topic name error.
If you are not seeing anything published, even to error, then I have to question whether your Home Assistant has a functional connection to your MQTT Broker.
Go to Developer Tools > MQTT, fill in the form with the data shown in the image, then click Publish.
You should see that hello was published to test.
Also, what are you using to confirm payloads have been published to topics? Are you using a tool like MQTT Explorer?
My MQTT broker is working fine, dedicated RPi with mosquitto installation.
I am monitoring the MQTT topic in Developer->MQTT and on my ESP32 that I am programming.
If youâre not interested in performing the test I suggested then my involvement ends now. You say it is âworking in principleâ but I need confirmation it is âworking in factâ.
I figured the last bit out myself. The problem was not with the automation or the triggering event, but with the template expansion. The topics inside the conditional scope need to go into double curly brackets:
topic: >-
{%- set pos = states('sensor.rollershutter_1_bathroom_position') | int %}
{%- set badog = states('input_select.rollershutter_badog') | int %}
{%- if pos == 100 and badog < 100 ) %}
{{"1house/bathroom/rollershutter/1/drive/up"}}
{%-elif pos == 0 and badog > 0 ) %}
{{"2house/bathroom/rollershutter/1/drive/down"}}
{%-elif pos < badog %}
{{"3house/bathroom/rollershutter/1/drive/up"}}
{%-elif pos > badog %}
{{"4house/bathroom/rollershutter/1/drive/down"}}
{% else %}
{{"error"}}
{% endif %}
mate, you havenât fixed it then as your code is not valid (at least what you posted above), extra right brackets at if and the first elif
and you spotted the last bit right but there is something fundamentally wrong with your approach, let me explain why.
Generally everything outside {{}} (templates) is passed as-is. That means if we look at the original code,
{%- if pos == 100 and badog < 100 ) %}
"1house/bathroom/rollershutter/1/drive/up"
will return
â1house/bathroom/rollershutter/1/drive/upâ
and it wonât work as expected at MQTT level because your MQTT topic is 1house/bathroom/rollershutter/1/drive/up, a different one (not sure if youâre allowed to have quotes in topics, wouldnât recommend that).
The easiest way to correct that is to remove quotes.
There are several alternatives: you can return something from a template. As you figured out, {{ "blah-blah"}} does the trick.
A very useful alternative is using format if you want to return dynamic strings, from simple
{{ "{}".format("error") }}
to things like
{# all these variables can (and should be) be dynamic #}
{% set house = '1house' %}
{% set room = 'bathroom' %}
{% set direction = 'up' %}
{{ "{}/{}/rollershutter/1/drive/{}".format(house, room, direction) }}
and youâll get the same result.
The bottom line the working code is
topic: >-
{% set pos = states('sensor.rollershutter_1_bathroom_position') | int %}
{% set badog = states('input_select.rollershutter_badog') | int %}
{% if pos == 100 and badog < 100 %}
1house/bathroom/rollershutter/1/drive/up
{%elif pos == 0 and badog > 0 %}
2house/bathroom/rollershutter/1/drive/down
{%elif pos < badog %}
3house/bathroom/rollershutter/1/drive/up
{%elif pos > badog %}
4house/bathroom/rollershutter/1/drive/down
{% else %}
error
{% endif %}