As I see there is no real “switch-case statement” in automation available.
I want to publish the payload of a mqtt msg depending on the current range of a sensor.
E.g.
If sensor value is between 1 and 5 publish payload A
If sensor value is between 6 and 8 publish payload B
If sensor value is between 9 and 10 publish payload C to topic
…
It’s an easy thing to this with multiple automations via conditions. But is it possible to put this to a single automation? Would be more “well-arranged”.
But when you get in to Jinja, it can also just be done like this:
action:
- service: mqtt.publish
data:
topic: mytopic/blah
payload: >-
{% set p = "A" %}
{% set s = states('sensor.mysensor')|float(0) %}
{% if s >= 6 and s <= 8 %}
{% set p = "B" %}
{% elif s >= 9 and s <= 10 %}
{% set p = "C" %}
{% endif %}
{{ p }}