Switch with two different scripts for on/off

Hi Everyone, I just set up hassbian on raspberry pi yesterday and am amazed with the possibilities!

I realise there is a similar topic to mine, but unfortunately it doesn’t answer my question, because the OP found a different way to solve his issue.

I am trying to configure a switch which runs two different scripts for on and off, or a script that would check the boolean and do one thing or the other.

In the perfect world, I would be able to do this:

homeassistant:
 customize:
   switch.projector_screen:
     emulated_hue_name: "The Screen"

switch projector_screen:
 script_on: script.screen_down
 script_off: script.screen_up

Is there anything like this available? I want to be able to tell Alexa Alexa, turn the screen on and Alexa, turn the screen off.

Below is what I got in my config file, I’m stuck because I don’t know how to do:

if (x is true) then do YYYY else do ZZZZ

From my research, it looks like you can only do if (x is true) then do YYYY else do nothing

input_boolean:
  screen_open:
    name: Projector Screen Open
    initial: off
    icon: mdi:projector-screen


script:
  screen_down:
    sequence:
      - service: switch.turn_on
        entity_id: switch.projector_screen
      - delay: 00:00:01
      - service: switch.turn_on
        entity_id: switch.projector_screen
      - service: input_boolean.turn_on
        data:
        entity_id: input_boolean.screen_open
      - delay: 00:00:35
      - service: switch.turn_off
        entity_id: switch.projector_screen
  screen_up:
    sequence:
      - service: switch.turn_off
        entity_id: switch.projector_screen
      - delay: 00:00:01
      - service: switch.turn_off
        entity_id: switch.projector_screen
      - delay: 00:00:35
      - service: input_boolean.turn_off
        data:
        entity_id: input_boolean.screen_open

switch:
 platform: rpi_rf
 gpio: 17
 switches:
  projector_screen:
   pulselength: 363
   protocol: 1
   code_on: 16632836
   code_off: 16632833 
  projector_screen_open:
   pulselength: 363
   protocol: 1
   code_on: 16632836
   code_off: 16632833
  projector_screen_close:
   pulselength: 363
   protocol: 1
   code_on: 16632833
   code_off: 16632836

Script description:
The screen needs two rf signals to be sent in order to unroll it, that’s why there is a 1 second delay. It can’t be unrolled fully because there isn’t enough space, so there is a 35 second timer.

I tried to implement tracking of the screen state with the boolean, hoping I can do if(open) then run screen_up else run screen_down.

All help greatly appreciated

Thanks,
Lucas

Use a Template Switch


Have it return the state of your boolean input in the value_template
1 Like

Thank you, do I understand correctly that if value_template: returns true it means the template switch is set to on?

I created this config, is that the correct way to think about it?

homeassistant:
  customize:
    switch.projector_screen:
      emulated_hue_name: "The Screen"

switch:
 - platform: template
   switches:
     projector_screen:
       value_template: "{{ is_state('input_boolean.screen_open', 'on') }}"
       turn_on:
         service: script.turn_on
         entity_id: script.screen_down
       turn_off:
         service: script.turn_on
         entity_id: script.screen_up

input_boolean:
  screen_open:
    name: Projector Screen Open
    initial: off
    icon: mdi:projector-screen

That’s correct.

your turn off is wrong; should be script.turn on…script.screen up. You can simplify both of these with just
service: script.scriptName

if you wanted you could even combine the up/down scripts and use a passed in variable to handle up/down :slight_smile:

1 Like

Thanks again for a quick reply, do you mean this: (I corrected the typo in the post above)

switch:
 - platform: template
   switches:
     projector_screen:
       value_template: "{{ is_state('input_boolean.screen_open', 'on') }}"
       turn_on:
         service: script.screen_down
       turn_off:
         service: script.screen_up

How do you pass variables? Then I guess you need an if(variable) then XXX else YYY inside the script, right?

Your modifications to the configurations look correct

Look here for how to pass/read variables in a script

Just got home and unfortunately it doesn’t work… getting this error:

2017-05-19 19:31:06 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: expected dict for dictionary value @ data[‘script’][‘screen_down’][‘sequence’][3][‘data’]. Got None
expected dict for dictionary value @ data[‘script’][‘screen_up’][‘sequence’][4][‘data’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 93). Please check the docs at https://home-assistant.io/components/script/
2017-05-19 19:31:06 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.

There’s a problem with your scripts. Looks like it’s complaining about where you’ve got an empty data element when you’re trying to manipulate your input boolean

Thank you, it was all due to indenting errors - still trying to get a hang of it.

Everything works perfectly now, although I found out that the screen is taking a slightly shorter/longer time to unroll to a desired level, so I just ordered a proximity sensor - adventure continues!

Hi,

So… How did it end? :slight_smile: I would like to know.

I would like to have a switch, too - where my “On” and “Off” is two different scripts.

It works perfectly thanks a lot !

You have to put in Configuration.yaml the switch and specify the ON and OFF and use maybe an input next to it.

# Boolean Presence
input_boolean:
  xxx_switch:
    name: XXX Switch
    initial: off

switch:
  - platform: template
    switches:
     lights_switch:
       value_template: "{{ is_state('input_boolean.xxx_switch', 'on') }}"
       turn_on:
         service: script.turn_on
         entity_id: script.turn_on_light
       turn_off:
         service: script.turn_on
         entity_id: script.turn_off