Create a switch to run different scenes when turned on and off?

I created some Input Boolean switches to run different scripts or scenes depending whether switched on or off. They work OK in Home Assistant but for some reason don’t work at all under emulated hue with Alexa.

For example if I told Alexa to ‘turn on the Living Room’ it (should) turn on the Input Boolean switch and run the ‘on’ scene. And if I told Alexa to ‘turn off the Living Room’ it (should) turn off the Input Boolean switch and run the scene for off.

Here’s an example of the code I’m using:

input_boolean:
  living_room:
    name: "Living Room"


automation:
  - alias: "Living Room on selected"
    trigger:
      - platform: state
        entity_id: input_boolean.living_room
        to: 'on'
    action:
      service: scene.turn_on
      entity_id: scene.living_room_on

  - alias: "Living Room off selected"
    trigger:
      - platform: state
        entity_id: input_boolean.living_room
        to: 'off'
    action:
      service: scene.turn_on
      entity_id: scene.living_room_off

Is there any way of doing this without an Input Boolean switch? (Or a way to make input boolean switches work with Alexa?)

I tried just using scenes called ‘Living Room on’ and ‘Living Room off’, but it seems to confuse Alexa and she goes through the ‘I found more than one device with that name…’ thing.

We would have to see how your emulated hue component is set up, but by default input_boolean should work with Alexa. I have to admit that I found the emulated Hue environment too restricted, and have implemented a skill instead. If you go the skill route, there’s virtually no limit to what you can do. It’s not as hard as it looks, but you do have to expose your HA instance to the internet.

Here’s my Emulated Hue config

emulated_hue:
  type: alexa
  host_ip: PI-IP-ADDRESS
  exposed_domains:
    - group
    - input_boolean
    - scene
    - script
    - switch

If you need any more info let me know

I have a Pi exposed to the internet and tried setting up it up as a skill, but had a problem with Amazon/Alexa talking to HA

All I can tell you is that should work, but again, I don’t use emulated Hue, I use the skills.

OK, thanks.

So you don’t know a way of creating a different type of switch to do the same thing? (switch template or something?)

Here is a switch template example that turns off a scene.

Thanks, it looks like it should be possible with that.

Although I’m not sure how I’d adapt it to my simpler switch (just turning on/off scenes or scripts).
I don’t think my understanding of Home Assistant is quite up to that :sweat_smile:

The alternative is to use automation and input select like here. The automation will run your scene when input select goes on and run a scene when it goes off.

The template switch should work but you need to decide how you want its state to appear. If it doesn’t matter how it appears, then you should replace value template with anything.

switch:
  - platform: template
    switches:
      my_scene_switch:
        value_template: "{{ is_state('my_scene_switch', 'on') }}"
        turn_on:
          service: scene.turn_on
          entity_id: scene.my_scene_on
        turn_off:
          service: switch.turn_off
          entity_id: scene.my_scene_off

Thanks a lot for this. It’ll most probably just be used for emulated hue, so won’t matter what it looks like.

I’ll give it a try in the morning, and see if I can get it up and running.

As a follow up to this I tried the test example you wrote and it works really well (and works fine when activated via emulated hue with Alexa) :slight_smile:

I just had to alter the bottom part to

turn_off:
  service: scene.turn_on
  entity_id: scene.my_scene_off

instead of

   turn_off:
      service: switch.turn_off
      entity_id: scene.my_scene_off

So I’ve now done this for all the scenes/scripts I was trying to use previously with Input Boolean (and for some reason wouldn’t work with Alexa).

So thanks a lot (again) for this!

I can’t seem to get this working :frowning:
I tried many things.
Error I get now is:
2017-11-11 17:15:55 ERROR (MainThread) [homeassistant.config] Invalid config for [switch.template]: [Badkamer_Verwarming] is an invalid option for [switch.template]. Check: switch.template->switches->Badkamer_Verwarming. (See ?, line ?). Please check the docs at https://home-assistant.io/components/switch.template/

And this is what i have in my config:

switch:
  - platform: template
    switches:
      Badkamer_Verwarming:
        value_template: "{{ is_state('switch.Badkamer_Verwarming', 'on') }}"
        turn_on:
          service: scene.turn_on
          data:
            entity_id: scene.badkamer_aan
        turn_off:
          service: scene.turn_on
          data:
             entity_id: scene.badkamer_uit

I also tried:

switch:
  - platform: template
    switches:
      Badkamer_Verwarming:
        value_template: "{{ is_state('Badkamer_Verwarming', 'on') }}"
        turn_on:
          service: scene.turn_on
          data:
            entity_id: scene.badkamer_aan
        turn_off:
          service: scene.turn_on
          data:
             entity_id: scene.badkamer_uit

Anyone has any idea?

Try this

switch:

  - platform: template
    switches:
      badkamer_verwarming:
        value_template: "{{ is_state('badkamer_verwarming', 'on') }}"
        turn_on:
          service: scene.turn_on
          entity_id: scene.badkamer_aan
        turn_off:
          service: scene.turn_on
          entity_id: scene.badkamer_uit

Any good?

So capitals letters are not allowed. No errors now but…when I flip the switch it does trigger the scene (badkamer_aan), however the switch flips rigth back to the off state.
So I can’t flip it off manually to run the turn_off scene.
Any idea about that? :slight_smile:

That’s right, you only use lowercase for the code (but can use uppercase letters, spaces etc. in the friendly_name under customize).

Not sure why it would keep switching off. Is there anything else in your code somewhere else that’s could be causing that?

You’ve got a circular reference here. The value template says if this switch is “on”, set the status of this switch to “on”. That’s not going to work.

If you want to stick with a template switch, change the template to something that changes state with your scenes.

I don’t understand your last sentence.

My goal is that I have 1 switch to “toggle” the running of 2 separate scenes.
When turned on it runs the scene that turns my heater on and when turned off it should run the scene that turns my heater off.

I thought reading the earlier posted code would do that.

Let’s say your heater is switch.heater.
Let’s say further that switch.heater is turned on with scene.badkamer_aan and switch.heater is turned off with scene.badkamer_uit.

The you could use this template to determine if the switch is on or off:

“{{ is_state(‘switch.heater’, ‘on’) }}”

Ah ok I understand. Problem is, the heater itself is not a switch. I set a temperature and then the zwave device opens/closes the valve more or less.
So the only reading I can do is the temperature.
If above 26 degrees then the (on) scene to put it on that setting was run.
If it’s below 15 the (off) scene ran to put it on that setting.

So you know how I can change that value template to check whether it’s above or below a certain temp?