Skip to content

Using Switcher in HomeAssistant

sagilo edited this page Mar 14, 2018 · 3 revisions

Basic

We will use 'command line switch'

Here is a switch example:

  - platform: command_line
    switches:
      switcher:
        command_on: "/srv/homeassistant/bin/python3 /home/homeassistant/switcher.py -m on -c /home/homeassistant/credentials.json"
        command_off: "/srv/homeassistant/bin/python3 /home/homeassistant/switcher.py -m off -c /home/homeassistant/credentials.json"
        command_state: "/srv/homeassistant/bin/python3 /home/homeassistant/switcher.py -m get_state -c /home/homeassistant/credentials.json"
        friendly_name: Switcher

Let's explain:

  • /srv/homeassistant/bin/python3 is the python path, I use virtual env to run HA so here I use the same path.
  • Absolute path to the script
  • Script mode (-m)
  • Absolute path of credentials.json, since we can't know for sure where the script will be executed from, we can't be sure credentials.json file will be next to it, hence, mentioning the path.

Notice: For command_state, HomeAssistant checks the application return code.
0 means the switch should be ON and 1 means OFF.
This is obviously reflected in the script and 0 will be returned when Switcher is ON, otherwise, 1.

Advanced

It's also possible to create a Switcher group with more options

Switcher group

First we will configure input_number:

input_number:
  switcher_operation_time:
    name: Time
    icon: mdi:clock
    mode: box
    initial: 30
    min: 5
    max: 90
    step: 5

I used 5 minutes steps with range of 5 to 90 minutes, but that's up to you.

Defining the shell_command to accept start command with argument:

shell_command:
  switcher_on:  "/srv/homeassistant/bin/python3 /home/homeassistant/switcher.py -m on -t {{delay}} -c /home/homeassistant/credentials.json"

Configure the switch which will use the input_number value and call the shell command:

switch:
  - platform: template
    switches:
      switcher_by_slider:
        friendly_name: Switcher
        value_template: "{{ is_state('switch.switcher', 'on') }}"
        turn_on:
          service: shell_command.switcher_on
          data_template:
            delay: '{{ states.input_number.switcher_operation_time.state | int }}'
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.switcher
  • If you already have switch configured, just add it below without the first line.
  • Notice this uses the switch.switcher we defined in the beginning of the tutorial

Sensor to get operation statistics:

Here I count the operation time from 5 am on the same day, but you can change it however you want If you do, make sure you comply with all mandatory fields (see here)

  - platform: history_stats
    name: Switcher ON statistics
    entity_id: switch.switcher
    state: 'on'
    type: time
    # starting at 05:00
    start: '{{ now().replace(hour=5).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

Finally, defining the group in groups.yaml

  switcher:
    name: Switcher
    entities:
     - sensor.switcher_on_statistics
     - input_number.switcher_operation_time
     - switch.switcher_by_slider

Have fun.