I have several HS300 wifi power strips that i have been holding off on deploying until they get added into HA. i know the backend is there, but someone needs to rework the HA python files. not being a python developer, i had to go with what i know. I know linux and shell scripting. so I wrote some scripts as a work around until proper support is added. I’m sharing in case anyone else finds this useful.
These are instructions for my setup, you will have to adapt to your specific setup.
-
ensure pyhs100 is installed
source /srv/homeassistant/bin/activate
pip install pyhs100 -
because the state and power usage for the entire strip is returned when called, i didn’t think it was very efficient to call for it for each individual plug. you get all of the same information each time.
so what i did was - using node red, run a script at a determined interval (5 minutes in my case) to dump the stats of the power strip. this gives me the state of each one and the power usage for each one.
I then parse that data and return it to HA via more scripts.
I call this script get_state.sh
#!/bin/bash
/srv/homeassistant/bin/pyhs100 --host 192.168.2.240 --strip state > /home/hass/192.168.2.240.txt
/srv/homeassistant/bin/pyhs100 --host 192.168.2.241 --strip state > /home/hass/192.168.2.241.txt
this is ran every 5 minutes via node red.
i added this to shellcmds.yaml
wifistrip_status: /home/hass/get_state.sh
and use the HA node red integration to run it every 5 minutes. you can run it as much as you like. but typically super granular stats are not really needed in my case. so 5 minutes is fine.
if you are not using node red, then use your preferred method to run a shell script at whatever time interval you want.
now that we have the plug state and power usage for each power strip dumped. we then parse the data for each one in HA
- in sensor.yaml i have the following (i’m only posting the first 2, copy it up to 6)
- platform: command_line
name: Net_Plug1
command: "/home/hass/read_pwr.sh 192.168.2.240 plug1"
unit_of_measurement: "W"
- platform: command_line
name: Net_Plug2
command: "/home/hass/read_pwr.sh 192.168.2.240 plug2"
unit_of_measurement: "W"
this reads the wattage for each plug into HA, it’s returned in MW, but i convert in the read_pwr.sh script. which looks like this…
#!/bin/bash
plug1=`cat /home/hass/$1.txt | awk -F'1:' '{print$1}' | tail -1 | cut -f3 -d, | cut -f2 -d: | awk '{print$1/1000}'`
plug2=`cat /home/hass/$1.txt | awk -F'2:' '{print$1}' | tail -1 | awk -F'1:' '{print$2}' |cut -f3 -d, | cut -f2 -d: | awk '{print$1/1000}'`
plug3=`cat /home/hass/$1.txt | awk -F'3:' '{print$1}' | tail -1 | awk -F'2:' '{print$2}' |cut -f3 -d, | cut -f2 -d: | awk '{print$1/1000}'`
plug4=`cat /home/hass/$1.txt | awk -F'4:' '{print$1}' | tail -1 | awk -F'3:' '{print$2}' |cut -f3 -d, | cut -f2 -d: | awk '{print$1/1000}'`
plug5=`cat /home/hass/$1.txt | awk -F'5:' '{print$1}' | tail -1 | awk -F'4:' '{print$2}' |cut -f3 -d, | cut -f2 -d: | awk '{print$1/1000}'`
plug6=`cat /home/hass/$1.txt | awk -F'5:' '{print$2}' | tail -1 | cut -f3 -d, | cut -f2 -d:| awk '{print$1/1000}'`
echo -e "plug1:$plug1\nplug2:$plug2\nplug3:$plug3\nplug4:$plug4\nplug5:$plug5\nplug6:$plug6\n" | grep $2 | cut -f2 -d:
- switches
In switch.yaml we control each plug and get the state of each plug. (again only posting the 1st two. copy it up to 6)
- platform: command_line
switches:
netplug1:
command_on: '/home/hass/too.sh 192.168.2.240 on 1'
command_off: '/home/hass/too.sh 192.168.2.240 off 1'
command_state: '/home/hass/read_state.sh 192.168.2.240 1'
value_template: '{{ value == "ON" }}'
friendly_name: Net Plug 1
netplug2:
command_on: '/home/hass/too.sh 192.168.2.240 on 2'
command_off: '/home/hass/too.sh 192.168.2.240 off 2'
command_state: '/home/hass/read_state.sh 192.168.2.240 2'
value_template: '{{ value == "ON" }}'
friendly_name: Net Plug 2
too.sh (turn on off) script looks like this
#!/bin/bash
/srv/homeassistant/bin/pyhs100 --host $1 --strip $2 $3
read_state.sh looks like this
#!/bin/bash
cat /home/hass/$1.txt | grep "Plug $2" | head -n 1 | cut -f2 -d:
I need to go back and name the plugs for what is connected to them (i.e. tv, xbox, etc) but this was just a proof of concept deal.
yes i know it’s ugly and hacky but it works.