I have a Shelly Plus 2PM relay, where I have set up schedules on the Shelly to turn the relay on/of at certain times of the day. I prefer to do this on the device itself so that the schedule will run without being reliant on network connectivity or my Home Assistant server being operational.
The 2PM relay on/off switches show up automatically in HA, but there are no automatic entities to control the internal schedules.
I found this page here which gave a good starting point into how to create a REST switch and the Shelly API to manage this: Managing Shelly relay schedules remotely from Home Assistant – James Nimmo
However, this guide is for the first generation shelly relays, and does not work for the Shelly Plus 2PM. The 2PM uses the gen2 API here: Welcome! | Shelly Technical Documentation
For anyone else trying to do a similar thing, here are working REST switch configurations that enable/disable the first two schedules on a 2PM:
switch:
- platform: rest
name: "Irrigation.Fruit.On1"
resource: http://192.168.50.171/rpc
method: post
headers:
Content-Type: application/json
body_on: '{"id":1,"method":"Schedule.Update","params":{"id":1,"enable":true}}'
body_off: '{"id":1,"method":"Schedule.Update","params":{"id":1,"enable":false}}'
state_resource: http://192.168.50.171/rpc/Schedule.List
is_on_template: "{{ value_json['jobs'][0]['enable'] }}"
- platform: rest
name: "Irrigation.Fruit.Off1"
resource: http://192.168.50.171/rpc
method: post
headers:
Content-Type: application/json
body_on: '{"id":1,"method":"Schedule.Update","params":{"id":2,"enable":true}}'
body_off: '{"id":1,"method":"Schedule.Update","params":{"id":2,"enable":false}}'
state_resource: http://192.168.50.171/rpc/Schedule.List
is_on_template: "{{ value_json['jobs'][1]['enable'] }}"
In the above example, first I have created 2 schedules for the first relay using the Shelly 2PM web interface (accessible from a handy link from the HA device info on your shelly). The first schedule turns the relay on at a certain time, the second schedule turns it off at a certain time.
The code above creates two switches, the first which enables/disables the On schedule, the second which enables/disables the Off schedule, allowing me to turn the automatic scheduling on/off from HA.
Some quick notes on the code:
- A separate resource and state_resource link is required, as in the 2PM setting the state and getting the state require 2 separate api calls
- In the body_on/body_off the first id can be set to what you like. The second id is the important one - it must match the id of the schedule that you want to control. In the 2PM the schedules are numbered from values incrementing from 1. You can find the number for a particular schedule by clicking on in from the Shelly web interface. Note that these ids are global, rather than per relay.
- The is_on_template extracts the enabled state from the Schedule.List API call json. In value_json[‘jobs’][X][‘enable’], X is an index into the array of the schedules, and so must be the id-1.
- I don’t think the headers section is strictly required
- Obviously you need to update the IP address to match your device, and you need to make sure you have static address assigned.