How to fire an action and set an input_boolean with one button/toggle in Lovelace?

Am a month or so in with HA and it’s a revelation - all the developers have done an amazing job on this very cool, functional open source software. The thing I’m stuck on today though is how to set an input_boolean state when I toggle a light switch in Lovelace. Any thoughts on how to do this?

I’ve looked at the input_boolean docs and searched the forums but couldn’t find anything.

The precise use case is that I want to record that a light is turned on manually (the input_boolean) rather than by automation (by motion sensor). This will allow me to add a condition to not automatically turn a light off, if it had been turned on manually.

The tap action of a button can only do one thing, so you need the tap action to fire a script and the script to do what you want.

script:
  my_lovelace_button:
    sequence:
      - service: input_boolean.turn_on
        entity_id: input_boolean.my_boolean
      - service: light.turn_on
        data:
          entity_id: light.1
          brightness_pct: 50
          color_name: blue

And then you can either just add that script as a button which will automatically fire when you press it, or use another kind of lovelace element and set it’s tap action to fire the script.

Hope this helps.

Thanks! As I’ve got a number of rooms (light groups) to configure like this, do you think this one script could be generalised to accept variables from the Lovelace card?

Depends on how specific each control needs to be, if it’s as easy as it being the same instruction set for each one then yeah…

script:
  my_lovelace_button:
    sequence:
      - service: input_boolean.turn_on
        entity_id: "{{ boolean }}" 
      - service: light.turn_on
        data:
          entity_id: "{{ light }}" 
          brightness_pct: 50
          color_name: "{{ color }}"

Then in lovelace

type: button
tap_action:
  action: call-service
  service: script.my_lovelace_button
  service_data:
    boolean: input_boolean.my_boolean
    light: light.1
    color: blue

(that’s off the top of my head, the syntax for the lovelace bit might not quite be right, but you get the gist)

1 Like

That’s clever Marc, thanks.

1 Like