Simple Toggle switch for HA scripts

Currently in the process of moving over 70 odd devices and several automations to HA from domoticz so forgive me if this is a stupid question

I have several HA scripts now that I set as on/off switches (e.g. script_ht_aircon_on is script 1 and script_ht_aircon_off is script 2)

I tried command line switch in switches.yaml

- platform: command_line
  switches:
    study_aircon:
        command_on: script.script_ht_aircon_on
        command_off: script.script_ht_aircon_off
        friendly_name: "Study AC"

That didn’t work. The switch shows up but returns a command failed error in the logs (I can manually execute the script from the front-end and they work as expected)

but these need to be tied back to HomeKit hence the need to set them as switches.

I would have guessed that having the ability to create a simple switch on a HA platform would be one of the first things - where am I going wrong?

Hey superczar,

if I read the page correctly, you need to provide an actual shell command for the command_line switch.
You probaly want a “Template Switch”: https://www.home-assistant.io/integrations/switch.template/
You should read this page, however I’ve written your switch as a Template here:

- platform: template
  switches:
    study_aircon:
        turn_on:
         service: script.script_ht_aircon_on
        turn_off:
          service: script.script_ht_aircon_off
        friendly_name: "Study AC"

The Template Switch also gives you access to a lot more, like availability templates etc.
Please report back if you had success!

The above returns a config error:
It seems template switches require a value template

‘
Invalid config for [switch.template]: required key not provided @ data['switches']['study_aircon']['value_template']. Got None. (See ?, line ?). 
YAML configuration reloading

Thanks for pointing me to the right direction though - should be able to figure it out hopefully.
Gotta say one thing though - love the front end of HA + the much better state of integrations.
However I still feel that a simple use case of creating a basic switch should have been a tad more straightforward :frowning:

It should be pretty easy, is there a reason you need to call a script to turn on your AC? I guess setting a temperature and all that, eh? Mind sharing your two scripts?
Home Assistant needs to know what the current state is - is there a way you can read that data somehow? Does the AC report its status?
If not, you could cheat a little by creating an input boolean (Under Configuration - Helpers) and using that as the value_template. Since that can get out of sync it’s a little wonky.
Probably better to use some sort of button - press button 1 to turn on, and button 2 to turn off, seems to fit your use-case a little better :slight_smile:

Actually there are a couple of very specific use cases and all are due to logitech harmony
In 2 of my rooms (that use a Tasmota IR ), I was able to use a pretty nice component from github that allows the HVACs to be setup as climate devices letting me use a nice control wheel to set temps/ modes etc

However 2 other rooms use a logitech harmony that require a specific device command to be issued - e.g.


data_template:
  command: PowerToggle
  device: '69714086'
  entity_id: remote.home_theatre
service: remote.send_command


These are the ones that are causing trouble

Yeah, that’s what I thought…
I’d really suggest using buttons for that since HA can’t get the state of the device.
If you still want to use a switch, I can guide you through it, however it may get out of sync with the actual device if something goes wrong.

I will try going the button route - although I am not sure if homekit will interpret it correctly.
Would be incredibly helpful if you could tell me a little more about the input_boolean method so that I can at the very least use it as a workaround for now while I work on porting the remaining devices

The button is just a frontend thing.
If there are no delays in your scripts, you can add “entities” in Lovelace and add the scripts as entities.
There should be an “EXECUTE” at the right hand side.
Alternatively you can use a (picture) glance and have the buttons map to call your scripts.
Or you could make bigger buttons by adding a “Button” in Lovelace. Then edit the on-tap action to call your script.

So the input boolean (as the name suggests) is basically just an internal, virtual switch.
You can create one by going to Configuration - Helpers (e.g. call it “ac”), you now have a input_boolean.ac entity.
Add a homeassistant.turn_off action that turns off input_boolean.ac to your script that turns your AC off (and a “turn_on” vice-verca)

Then, try using this:

- platform: template
  switches:
    study_aircon:
        turn_on:
         service: script.script_ht_aircon_on
        turn_off:
          service: script.script_ht_aircon_off
        friendly_name: "Study AC"
        value_template: "{{ is_state('input_boolean.ac', 'on') }}"
1 Like

Thanks - Your solution worked perfectly.
What’s even better is that other than creating the input_boolean through code, I could do the rest from the UI directly.
What I did was
a) Create an input_boolean say input_boolean.ht_ac
b) Used lovelace UI to create two automations that link the state of the above boolean to a service call- which triggers the necessary scripts

1 Like

Oh whoops, yeah that’s right. There’s no need for an additional switch since you can just use the input_boolean directly, sorry about that :crazy_face:

ok, so the final way with the input boelean created in the helper, then two automations that call service if the created input boolean changes, worked immediately.

However, anybody willing to help with me with my first template?

My code in configuration.yaml is

switch:
  - platform: template
    switches:
      projector:
        value_template: "{{ is_state('input_boolean.projector_helper', 'on') }}"
        turn_on:
          action: call-service
          service: shell_command.projector_on
        turn_off:
          action: call-service
          service: shell_command.projector_off
        friendly_name: "Projector"

Shell commands work, tested elsewhere.
Then I have the input boolean but I do not understand how do I change the state by button. In the solution, fedot says:

Add a homeassistant.turn_off action that turns off input_boolean.ac to your script that turns your AC off (and a “turn_on” vice-verca)

I did this by the common button in lovelace linked to the input boelean entity. Nothing runs, button state changes.

1 Like

Looking back at my original suggestion I don’t even know what I was writing :confused:
You won’t need an input_boolean for that, just use the template switch.
So remove the value_template and delete the projector_helper.
As for your switch, take a look at the documentation. Remove the action: call-service from your switch.

1 Like

For future reference: based on the suggestions fedot provided and samples Vahaldor wrote, here is the final working solution for my similar case using Scripts, this will show up as a separate on and off button next to each other with a bolt icon on the dashboard and works with the main/top card toggle switch aswell.

Configuration.yaml

 - platform: template
    switches:
        433mhz_i_toggle:
            turn_on:
                service: script.433mhz_i_on
            turn_off:
                service: script.433mhz_i_off
            friendly_name: "433MHz_I_TOGGLE"

In case you want to also trigger the same with a single physical button press on switch as toggle, this would be the trick:

automation

alias: Lightsaber toggle
description: “”
trigger:

  • platform: device
    device_id: <cleared_my_dev_id_from_here>
    domain: homekit_controller
    type: button1
    subtype: long_press
    condition:
    action:
  • service: switch.toggle
    data: {}
    target:
    entity_id: switch.433mhz_i_toggle
    mode: single

The called Scripts are looking like this for reference, having another one with OFF aswell, rest is the same:

script

alias: 433MHz_I_ON
sequence:

  • service: remote.send_command
    data:
    device: 433MHz_I
    command: “ON”
    num_repeats: 1
    hold_secs: 1
    delay_secs: 0.4
    target:
    entity_id: remote.broadlink_rm4pro
    mode: single
    icon: mdi:toggle-switch-outline