Amazon Dash Button - automation to toggle all devices on/off in a single script help

Hi

I have hacked my amazon button so it triggers an automation i have written in home assistant. This automation turns everything off in my house.

The thing is, as i can only add one action to a dash, it wont turn everything back on once iv turned everything off. Its simply an off button for everything currently.

Im looking to try and automate this by using a lamp as the state to see whether thats on and call the appropriate on/off automation script. I never want this automation to fire unless i call it, which is why i used media_player.kodi as i never use it now.

At the moment, i have 1 automation to turn off all devices, one to turn on all automations, and one which makes the decision which is what the dash button will call - which is where i am stuck.

There might be an easier way to do this so im all ears!
here’s what i have;

automation dash button off:
alias: dash off
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: homeassistant.turn_off
  entity_id: light.large_l,light.small_l,light.stairs,light.kitchen,switch.testing_hyperion,media_player.sony_tv
- service: homeassistant.turn_on
  entity_id: switch.kodi_input_amp

automation dash button on:
alias: dash on
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: homeassistant.turn_on
  entity_id: light.large_l,light.small_l,light.stairs,light.kitchen,switch.testing_hyperion,media_player.sony_tv
- service: homeassistant.turn_on
  entity_id: switch.television_input_amp

automation dash button:
alias: Dash button - all off
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: automation.trigger
  data_template:
    entity_id: >-
      {% if is_state('light.large_l', 'off') %}
        automation.dash_on
      {% else %}
        automation.dash_off
      {% endif %}

Make two scenes. One that turns on all of your devices, and one that turns off all your devices. Call them dash_on and dash_off.

Then, write an automation that triggers based on the dash button (doesn’t look like you have that part in your example) and using a data template for the light as you’ve done should work for the action.

I’m a bit mystified about the media player condition - what’s that for?

Try:

- alias: Dash button
  trigger:
    - [here you need to put whatever the dash button does]
  action:
    - service: scene.turn_on
      data_template:
        entity_id: >-
       {% if is_state('light.large_l', 'off') %}
         scene.dash_on
       {% else %}
         scene.dash_off
       {% endif %}

ah thanks mate, ill try using a scene.

I was only using kodi i was going down the automation calling another automation route, so i needed something that wouldnt actually fire but could be triggered. But your examples makes alot more sense!

Just trying it now, ill let you know how i get on.

- [here you need to put whatever the dash button does]

Not wuite sure what to put here, my dash button basically sends json and i have dasher listening for any messages:

{"buttons":[
{
"name": "Turn on Script from Dash Button",
"address": "74:c2:46:xx:xx:xx",
"timeout": "60000",
"protocol": "udp",
"url": "https://duckdns.org:8123/api/services/automation/trigger",
"method": "POST",
"headers": {"x-ha-access": ""},
"json": true,
"body": {"entity_id": "automation.dash_button__all_off"}
}

]}

I have also edited my first post to contain everything i was trying before,

Thanks mate

My approach did work, just needed tweaking:

automation dash button off:
alias: dash off
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: homeassistant.turn_off
  entity_id: light.large_l,light.small_l,light.stairs,light.kitchen,switch.testing_hyperion,media_player.sony_tv
- service: homeassistant.turn_on
  entity_id: switch.kodi_input_amp
automation dash button on:
alias: dash on
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: homeassistant.turn_on
  entity_id: light.large_l,light.small_l,light.stairs,light.kitchen,switch.testing_hyperion,media_player.sony_tv
- service: homeassistant.turn_on
  entity_id: switch.television_input_amp
automation dash button:
alias: Dash button - all off
initial_state: True
hide_entity: False
trigger:
platform: state
entity_id: media_player.kodi
to: 'stopped'
action:
- service: automation.trigger
  data_template:
    entity_id: >-
      {% if is_state('light.large_l', 'on') %}
        automation.dash_off
      {% else %}
        automation.dash_on
      {% endif %}

Now I understand! The dash button just sends a POST. Still not sure I understand the media player but I’m glad it’s working!

Aren’t triggers compulsory - I thought that they were? That’s why I added it

Yes they are, but that’s why I suggested originally that you should be using a script for this rather than an automation. And then in your dash button config you’d replace automation/trigger with script/turn_on like so:

{"buttons":[
{
"name": "Turn on Script from Dash Button",
"address": "74:c2:46:xx:xx:xx",
"timeout": "60000",
"protocol": "udp",
"url": "https://duckdns.org:8123/api/services/script/turn_on",
"method": "POST",
"headers": {"x-ha-access": ""},
"json": true,
"body": {"entity_id": "script.your_script_name_here"}
}
]}

Oi see.

But I needed automation to detect the condition/state of the light before it fires, which I’m pretty sure a switch doesn’t do?

Thanks for the clarity though!

You can have conditions in scripts: see https://home-assistant.io/docs/scripts/. Conditions are evaluated in order and execution of the script only continues if the condition returns “true”.