Problem passing variable from automation to script

New readers, please skip this first post, and jump down to my post 27 september 2019. Thanks.

Hello all

I’m using the custom:button-card with great success - and thanks @RomRider, for the card !

I’m struggling however to make it execute a script. The script itself works, if I trigger it from within HA UI, but not from my lovelace card. I hope anyone can help me with the answer.

First and foremost, I would like to execute the variable update directly from the custom:button-card, but if not possible I would as a secondary option like to call my “set variable value” script with a argument.

  - type: "custom:button-card"
    icon: mdi:volume-high
    show_label: false
    size: 50%
    color: auto
    tap_action:
      call-service:
      service: script.turn_on
      service_data:
        entity_id: var.menu_click
        value: 'Ju-Huu'

      action: navigate
      navigation_path: /lovelace/musik_volume
    styles:
      card:
        - height: 100px
        - width: 100px
        - background-color: rgba(0, 0, 0, 0.5)
#        - background-color: >
#            [[[
#              if (states['var.menu_click'].state ='Test')
#                return "white";
#              return "red";
#            ]]]

/Michael

I don’t know what you want to try with the script toggle in your config i.e. “value”?.
And i also see you’re using a navigate path in the same tap_action, i don’t think this will work. (can be wrong)

Why not use one script, to fire up the next script AND set the variable?

Nevertheless, i “only” use the following code which works like a charm to trigger my script in the button card code.

            tap_action:
              action: call-service
              service: script.toggle
              service_data:
                entity_id: script.1555789917776
    tap_action:
      action: call-service
      service: script.myscript
      service_data:
        var1: var.menu_click
        var2: 'Ju-Huu'

You didn’t give enough info, like the name of the script and the variables it uses. So I made them up for the example.

Next time, post your script and we’ll be able to help more.

EDIT: I guess you could be using the custom variable component. So that would look like this

    tap_action:
      action: call-service
      service: variable.set_variable
      service_data:
        variable: menu_click
        value: 'Ju-Huu'

Hello all

Thanks for the replies so far. I believe have tried tthem all, even mixing them. It seems to me that the variable value is not coming over from the automation.

I have made a very simple test, but still cannot get it to work.

Automation:

  - type: "custom:button-card"
    icon: mdi:food-variant
    show_label: false
    size: 50%
    color: auto
    tap_action:
      action: call-service
      service: script.menu_skift 
      data:
#        variables:  <- it makes no difference if this line is there or not
        valgtmenu: 'kokken'

Script:

  menu_skift:
    alias: "menu skift"
    sequence:
      - service: variable.set_variable
        data_template:
          variable: menu_click
          value: "{{ valgtmenu }}"

This runs without error, but clears the current variable value.

If I in the script put in:

value: ‘Test’ instead

it works just fine, also triggering it from the automation. So to me, either the automation is not sending, or the script is not receiving the variable with my current syntax.

/Michael

:man_facepalming:

This is not an automation… This is a lovelace card using a tap_action.

If you follow the button card documentation, you’ll notice that the format for calling a service is:

      tap_action:
        action: call-service
        service: media_player.volume_up
        service_data:
          entity_id: media_player.livimg_room_speaker

making this the correct format:

  - type: "custom:button-card"
    icon: mdi:food-variant
    show_label: false
    size: 50%
    color: auto
    tap_action:
      action: call-service
      service: script.menu_skift 
      service_data: #<------ NOTICE THIS LINE
        valgtmenu: 'kokken'

Hello Petro

It maskes absolutely no sense, me starting to explain how many diffent combinations I have tried, and ensure you that I have read the documentaion…

Your syntax works !

I’m ebarrased to bow my head and say thanks ! I have no idea, how this could be the missing link I didnt try. Maybe, I had it right, but at that time had the script wrong.

May I ask second question please. What I am trying to achieve, is :

“on_tap” :

  • Set a variable
  • navigate to another lovelace view.

I can’t however read from “custom:button-card” documentation how to do two actions “On_Tap”, as well as I dont think I can Navigate" from a HA script ?

What do you suggest ? Call a script from a script ?

I hope I’m not asking too much from your time. I so, I apologize, and please just ignore.

Michael