-
Notifications
You must be signed in to change notification settings - Fork 7
Using Switcher in HomeAssistant
sagilo edited this page Mar 14, 2018
·
3 revisions
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.
It's also possible to create a Switcher group with more options
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.
shell_command:
switcher_on: "/srv/homeassistant/bin/python3 /home/homeassistant/switcher.py -m on -t {{delay}} -c /home/homeassistant/credentials.json"
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
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() }}'
switcher:
name: Switcher
entities:
- sensor.switcher_on_statistics
- input_number.switcher_operation_time
- switch.switcher_by_slider
Have fun.