Hass.io Broadlink RM Switch send multiple commands

Fantastic find!

You can create a template switch with one script for on and the other for off. the switch can be called projector then you can turn on and off like normal switch

Is the below correct? I do not understand the value_template function -…?

I need all this because the sequence for the ON is different from the sequence of the OFF

script:
  projector_on:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.projector_power_plug
      - delay:
          milliseconds: 300
      - service: switch.turn_on
        data:
          entity_id: switch.epson
  projector_off:
    sequence:
      - service: switch.turn_off
        data:
          entity_id: switch.epson
      - delay:
          milliseconds: 300
      - service: switch.turn_off
        data:
          entity_id: switch.projector_power_plug

switch:
  - platform: template
    switches:
      projector:
        value_template: "{{ is_state('projector', 'on') }}"
        friendly_name: "Projector"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.projector_on
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.projector_off

  - platform: broadlink
    host: 192.168.1.19
    mac: 'xxxxxxxxxxxxf'
    switches:

      projector_power_plug:
        command_on: 'xxxxA'
        command_off: 'yyyyyy'
        friendly_name: 'Projector power plug'
      epson:
        command_on: 'zzzzzz'
        command_off: 'yyyyyy'
        friendly_name: 'Epson'

The value template tells the template switch what entity state or states to look at so it knows when it’s on. Typically that would be one entity in the script that’s turning on.

Example:

value_template: "{{ is_state('switch.projector_power_plug', 'on') }}"

Otherwise seems ok but am not at my computer so can’t double check

Sorry this part I don’t get it, entirely. I mean I need to do a switch off/on with different commands (different script in my case for off and for on), what does that has to do with looking at a state?

I did this but it breaks my switch component

2019-01-21 23:15:02 ERROR (MainThread) [homeassistant.components.switch] Error while setting up platform broadlink
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/switch/broadlink.py", line 157, in setup_platform
    device_config.get(CONF_COMMAND_OFF)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/switch/broadlink.py", line 194, in __init__
    self._command_off = b64decode(command_off) if command_off else None
  File "/usr/local/lib/python3.6/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

from this post, it seems the error is in the characters of the sequence which are wrong … but how to fix??

EDIT: I aded a = and that made the erro disappear

The template switch will take its state from the list of entities that are in the value template. I am at my computer now so can post an example.

  - platform: template
    switches:
      stereo:
        value_template: "{{ is_state( 'switch.hifi_switch_broadlink', 'on') }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.stereo_on 
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.stereo_off

So whenever or however my switch.hifi_switch_broadlink is turned on the template switch will update its status to on.

You could have more than one entity id’s listed if you want. This is another example

  - platform: template
    switches:
      everything_downstairs:
        friendly_name: Everything downstairs
        value_template: "{{ is_state( 'switch.hifi_switch_broadlink', 'on') and is_state( 'light.picture_light', 'on') }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.everything_downstairs_on
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.everything_downstairs_off
1 Like

Anyway I do not need to understand, as long as it works.

Now the switch has a slider, I can turn it ON and it works, but it stays in OFF position, so I do not know how to call the OFF part

That is exactly what the value template is for. So when home assistant has the state of switch.projector_power_plug as on then it will update the statues of the template switch to on.

Have you checked your scripts work?

1 Like

Ahh, now I think I got it, sorry I am very slow at these things. Checking now.

Nevertheless if I do not want that to be checked (i know if my projector is ON since it makes lot of noise and light, lol) isn’t possible to put an assumed_state so to have separate ON OFF like so many other switches?

my on script for stereo

stereo_on:
  alias: Stereo on
  sequence:
    - service: switch.turn_on
      entity_id: switch.hifi_switch_broadlink
    - delay: 00:00:05
    - service: media_player.turn_on
      entity_id: media_player.cyrus
    - delay: 00:00:03      
    - service: media_player.volume_up
      entity_id: media_player.cyrus
    - delay:
        seconds: 0.25
    - service: media_player.volume_up
      entity_id: media_player.cyrus
1 Like

You can use any entity you like I just happened to pick that one as its first on the list in the script. Like I said whenever the items listed in the value template are known by home assistant to be on then the template switch will assume it is also on.

Think of it like a virtual switch, that because it doesn’t have a known state of its own you have to tell it to use something elses state to base its assumption on

It will work exactly as you describe once its set up right.

1 Like

but it doesn’ in my case, I also checked on the template

{{ is_state(‘switch.projector_power_plug’, ‘on’) and is_state( ‘switch.epson’, ‘on’) }}

and gives True

that’s the script

  projector_on:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.projector_power_plug
      - delay:
          milliseconds: 300
      - service: switch.turn_on
        data:
          entity_id: switch.epson
      - service: media_player.turn_on
        entity_id: 
          - media_player.marantz
      - service: media_player.select_source
        data:
          entity_id: media_player.marantz
          source: 'Chromecast'
#########################
  projector_off:
    sequence:
      - service: switch.turn_off
        data:
          entity_id: switch.epson
      - delay:
          milliseconds: 1000
      - service: switch.turn_off
        data:
          entity_id: switch.projector_power_plug

that’s the switch

  - platform: template
    switches:
      projector:
        value_template: "{{ is_state('switch.projector_power_plug', 'on') and is_state( 'switch.epson', 'on') }}" 
        friendly_name: "Projector"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.projector_on
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.projector_off

If I call the service turn on switch switch.projector it works fine
also turn off switch.projector it works fine.

But in front end the slider stays on off …

Now it works I had to modify the above in

{{ is_state(‘switch.projector_power_plug’, ‘on’) }}

apparently the second switch goes ON with a delay, so the virtual switch does not updated in the right moment. Well at least I think that’s what happening

Yes thats exactly what is happening

1 Like

Nice find indeed, this worked for my Optoma projector that needs to press the power button twice. For what it’s worth if you want the code to repeat once you set the input to “1” not “2”. Thanks for sharing!

1 Like

I need to add a delay between each code, the below script will not run with delay tag on it.

`#samsung TV brightness preset
samsung_tv_picture_mode:
    alias: Samsung TV Picture Mode
    sequence:
      - service: broadlink.send
        data:
          host: 192.168.1.19
          packet:
            - "JgCMAJOWETkRORE5ERQRFBEUERUQFRA5ETkROREVERQRFBEUERQRFBE5ERQRORE5ERQRFBEUETkRFBE5ERQRFBE5ETkROREABgqUlhE5ETkROREUERQRFBEUERQRORE5ETkRFBEUERQRFBEUERQROREUETkROREUERQRFBE5ERQROREUERQRORE5ETkRAA0FAAAAAAAAAAAAAAAA"
            - delay:  00:00:05
            - "JgCMAJOWEjgRORM3ERUQFRITEhMRFBI4ETkROREUERQRFBITERQSExI4ERQSExITEzcTNxMSEzcTEhM3ETkROREUERQROREABguTlhI4ETkROREVEBUQFREUERQRORE5ETkRFBEUERQRFBEUERQROREUERQRFBE5ETkRFBE5ERQRORE5ETkRFBEUETkRAA0FAAAAAAAAAAAAAAAA"
            - delay:  00:00:05    
            - "JgBGAJOWETkRORE5ERQRFBEUERQRFBE5ETkROREUERQRFBEUERQRFBEUERQTNxEUETkROREUETkRORE5ERQROREUERQROREADQUAAA=="
            - delay:  00:00:05
            - "JgCMAJSWETkRORE5ERQRFBEUERQRFBE5ETkROREUERQRFBEUERQROREUERQRFBEUETkROREUERQRORE5ETkROREUERQROREABgqUlhE5ETkROREUERQRFBEUERQRORE5ETkRFBEUERQRFBEUETkRFBEUERQRFBE5ETkRFBEUETkRORE5ETkRFBEUETkRAA0FAAAAAAAAAAAAAAAA"
            - delay:  00:00:05    
            - "JgBGAJOWETkRORE5ERQRFBEUERQRFBE5ETkROREUERQRFBEUERQRFBEUERQTNxEUETkROREUETkRORE5ERQROREUERQROREADQUAAA=="
            - delay:  00:00:05        
            - "JgCMAJeTFDYUNhQ2FBEUERQRFBEUERQ2FDYUNhMSExITEhMSExITNxMSEzcTNxMSEzcTEhMSExITNxMSExITNxMSEzcTNhQABgiXkxQ2FDYUNhQRFBETEhQRFBETNxQ2FDYUERQRFBEUERQRFDYUERQ2FDYUERQ2FBEUERQRFDYUERQRFDYUERQ2FDYUAA0FAAAAAAAAAAAAAAAA"
`

I think you can only have delays between service calls. So I just do several send packet service calls with the delay in between

Thanks! any sample code to start with.