Pass payload/variable from automation to script


I’m trying to pass a variable from my MQTT automation - but I’m having trouble getting it to work, even when I hardcode the value in the automation.

This is my script:

            service: media_player.volume_set
            entity_id: media_player.stue
                volume_level: '{{ states.media_player.kkken.attributes.volume_level + my_volume }}'

This is my automation:

      - alias: Sonos Volume Up Stue from MQTT
          platform: mqtt
          topic: sonos/stue/volume_up
          payload: "1"
          - service_template: script.volume_up_stue
              variables: >-
                my_volume: '0.5'

Now, in time I would like to be able to set “my_volume” from MQTT or from another trigger, but for now I just want the 0.5 value passed to my script and getting that to work…

As I understand the documentation, variables set in a data_template should be available in the script that is called. But I’m still trying to figure out the difference om service_template and data_template, and where to put my variables.

Yeah… spend 5 minutes TRYING to get this editor to read my code formatted, and it ends up as plain text. Why is there not a guide on how to format code??

Found the answer myself on how to format codeblocks. Not very intuitive :smiley:

Have you TRIED reading the blue box on the top of EVERY page?

Yes I TRIED reading the box, but it just links to a generic Github-page explaining, vaguely, how to format code. What the blue box SHOULD have read, was just “paste three backticks before and after your code”.

Now, reading through many, many posts in this forum, my guess is that 80 % are not correctly formatted. Should we blame the users or the system? Maybe it’s just not obvious enough how to do it :wink:

You can select the text and use the </> icon to format.

That link on the box is new, the old one was better.

Apart from the wrong spacing on the script a can’t see anything wrong. Do you have any errors on your log?