Automation for shelly button integrated with mqtt

I try to create an automation for a shelly button who ist integrated with mqtt.

the batterie sensor of shelly button works fine, also I can see the jason statements of the shelly button in the mqtt-broker.

I found this code to do the automation, but it seems to be from an older version of HA:

I have now this code in the automations file:

- id: '9988770665544'
  alias: "Shelly Button1"
  trigger:
  -  platform: mqtt
     topic: "shellies/shellybutton1-D8F15B154F21/input_event/0"
  condition: []
  action:
  - service: script.turn_on
    data_template:
      entity_id:
        {% if trigger.payload_json.event == "S" %}
          script.kurz1
        {% if trigger.payload_json.event == "SS" %}
          script.kurz2
        {% if trigger.payload_json.event == "SSS" %}
          script.kurz3
        {% if trigger.payload_json.event == "L" %}
          script.lang
        {% endif %}

with this code i get this error:
image

has anyone a hint whats wrong?

Try changing the 2nd, 3rd and 4th "if"s to “elif”

    data_template:
      entity_id:
        {% if trigger.payload_json.event == "S" %}
          script.kurz1
        {% elif trigger.payload_json.event == "SS" %}
          script.kurz2
        {% elif trigger.payload_json.event == "SSS" %}
          script.kurz3
        {% elif trigger.payload_json.event == "L" %}
          script.lang
        {% endif %}

Thanks, but unfortunately error is still the same :disappointed_relieved:

image

Try adding a > after entity_id:

data_template: 
  entity_id: >
    {% if trigger.payload_json.event == "S" %}
1 Like

Doh !

That will do it !!

Hi jmiles

great, it works now as excpected :grinning:.

I’ve tried this allready yesterday, unfortunately I didn’t realized that I had changed the script aliases and not the enitiy-id, which is used to call a script… so, the if else structure worked fine, but no script was found an executed… :woozy_face:

Hi @wema
Can you explain me how this works?
What is the difference between this script (on what event is it started) and on the other 4 scripts (kurz1,2,3,long) ?

I’m not sure if I’ve understood your question :thinking:

On the Shelly Button you can do this 4 different actions:

  • press once
  • press twice
  • press triple
  • press long

each action give different data in the mqtt paylond:

This automatin is triggered when the payload changes,
and then depending of the sent data (S / SS / SSS or L) the defined script is executed.

The names of the scripts (kurz1, kurz2, kurz3, lang) are just examples without any sence.

Hi Martin

I understand that the button has 4 possible triggers, therefor you use 4 different scripts.
But what in what case is the script ‘script.turn_on’ used ?

Yves

“script.turn_on” is not a script, this is the service used to execute one of the 4 scripts.
Which one is defined in the data template.

these codes are similar, one for scripts and one for switches:

  - service: script.turn_on
    entity_id: script.lang
  - service: switch.turn_on
    entity_id: switch.shelly_1_keller

with conditions there is a very nice way to do this in the gui. and it works great!

create a new automation in the gui.
for trigger choose MQTT and set the topic as:
shellies/*yourshellytopic*/input_event/0
leave payload empty.
and under actions select “choose” and then for condition “template” and use:
{{ trigger.payload_json.event == "S" }} (or SS,SSS,L)
then you can add actions or add another options with other template conditions.

6 Likes

Thanks, Tobias
This was very helpful. this works great