Hi,
I have a Gosund SP111 smart plug (energy measuring) flashed with ESPhome to which I connected a Linux machine.
I would like to turn off the smart plug once the machine is powered off. I can do this by having ESPhome start monitoring (on request) the power consumption and by cutting power when it drops below a threshold.
However, how to send a command to ESPhome from Linux? since it happens during shutdown, it has to be something I can do from the command line.
On the other side, in ESPhome how can I setup an automation which gets triggered only when a certain command is received?
If this was me, I would be using MQTT. Publishing an MQTT message in Linux is easy ( apt-get install mosquitto-clients or whatever package manager you are using ) Publishing a message is then as simple as mosquitto_pub -h ip-address-of-mqtt-broker -t my/topic/for/the/shutdown/command -d "some data"
As for the ESPHome side of things, you can either implement a service that Home Assistant can call from an automation, or you can tell the ESPHome device to listen to MQTT too and then react when it sees the message that it is waiting for.
This seems a more straightforward solution, but I need to call a service different from the simple “turn_off”, since the Linux machine will need few more seconds to fully shutdown after the command is issued.
Can I call automations defined in esphome using this way?
The switch you call can be a template. Can’t do formatting on this phone but basically:
- platform: template
name: Whatever
id: whatever
turn_off_action:
- switch.template.publish:
id: whatever
state: OFF
- delay: 60s
# your other stuff here.
Your other stuff might be turning off the actual immediate switch, or start flashing a led, or whatever you need.
If it’s a gpio switch you’re calling, you can probably dispense with the template and just use a turn_off_action directly in the normal switch block.
In the normal switch I can have “on_turn_off” but it seems that the actions listed are performed after effective switching, so not what I need:
switch.on_turn_on / switch.on_turn_off Trigger
This trigger is activated each time the switch is turned on. It becomes active right after the switch component has acknowledged the state (e.g. after it switched ON/OFF itself).
So I used template but I get HTTP error 500 when I try:
web_server:
switch:
- platform: gpio
pin: GPIO14
id: relay
name: "HEVO - Switch"
icon: mdi:power-socket-eu
on_turn_on:
- output.turn_on: led
- switch.template.publish:
id: delayed_switch
state: ON
on_turn_off:
- output.turn_off: led
- switch.template.publish:
id: delayed_switch
state: OFF
- platform: template
id: delayed_switch
name: "HEVO - Delayed switch"
internal: true
turn_off_action:
- switch.template.publish:
id: delayed_switch
state: OFF
- delay: 10s
- switch.turn_off: relay```
I tried to operate the normal switch first:
http://192.168.xxx/gpio/relay/turn_on
http://192.168.xxx/switch/relay/turn_on
http://192.168.xxx/gpio/HEVO - Switch/turn_on
http://192.168.xxx/switch/HEVO - Switch/turn_on
None works. The IP I use is correct because without any path I get the ESP web interface.
Do I need something else?
The web server is active already.
The ID for the post request is created by taking the name of the component, stripping out all non-alphanumeric characters, making everything lowercase and replacing all spaces by underscores.
To confirm the <id> to use, you can set the log level to VERY_VERBOSE and check the object_id: in the logs.
Remember, you need to make a POST request. A get request to the domain and ID will tell you the status, but you need to POST to change it.