[SOLVED]Automation --> script problem

Hi,

i’m new to home assistant & been trying to figure some stuff out.

In automation i’m trying to pass an entity_id to a script.
In the script i try to get the state of the passed entity & execute a service based on that state.
I don’t get errors, it just doesn’t do anything.

How to debug these things ?

automation code:

  • id: ‘automation_12’
    alias: toggle multiple light on mqqt /arduino/input/#number# events with payload 2
    trigger:
    platform: mqtt
    topic: arduino/input/+
    payload: ‘2’
    action:
    • service: script.switch_owntoggle
      data_template:
      variables: >
      {% if trigger.topic.split(’/’)[2] == “32” %}
      var_entity: group.outputgrp01
      {% elif trigger.topic.split(’/’)[2] == “33” %}
      var_entity: group.outputgrp02
      {% endif %}

script code:

switch_owntoggle:
sequence:
- service_template: >
{% if is_state( var_entity , ‘off’) %}
switch.turn_on
{% else %}
switch.turn_off
{% endif %}
data_template:
entity_id: >
{{ var_entity }}

also: i can’t seem to figure out how to post code from notepad++ to this forum … the </> button only makes it worse

There used to be instructions at the top of the page but for some reason they have recently been removed. Basically, type a line containing just three “back ticks”, before and after the code. A “back tick” is the character that is typically the top-left most key on the keyboard. Looks like: `

So, first in your automation action, you can’t put a parameter name inside a template. Also, if you use the script name as the service you don’t need “variables:”. Lastly, always have an else in the if statement, otherwise, if all if’s & elif’s are false, you’ll probably have problems. So it should look like this:

  - service: script.switch_owntoggle
    data_template:
      var_entity: >
        {% if trigger.topic.split('/')[2]|int == 32 %}
          group.outputgrp01
        {% else %}
          group.outputgrp02
        {% endif %}

Having said that, it could probably be simplified to:

  - service: script.switch_owntoggle
    data_template:
      var_entity: "group.outputgrp{{ '01' if trigger.topic.split('/')[2]|int == 32 else '02' }}"

Your script looks fine, but it, too, can be simplified:

switch_owntoggle:
  sequence:
    service: switch.toggle
    data_template:
      entity_id: "{{ var_entity }}"
1 Like

@pnbruckner

thanks your changes to the automation did the trick. my problem is solved now. :smiley: :smiley:

the reason i’m using a script, is because the logic behind group.toggle is crap imho. (= it toggles the individual devices inside the group, not the group as a whole).
I think the simplified script will have the same issue’s as the original group.toggle i’ve attempted.

Ok, missed that. Glad it’s working. Might want to change to homeassistant.turn_on and off in case you ever put a mix of switches and lights in the group.