StyraHem / ShellyForHASS

Shelly smart home platform for Home Assistant
MIT License
620 stars 111 forks source link

[FR] Synchronise I/O URL actions #533

Open filmgarage opened 3 years ago

filmgarage commented 3 years ago

The problem: Yesterday was one of the very rare occasions that my Home Assistant server froze. However it is wonderful to see that most of my lights still work as they should because they are still hardwired from the switches to the Shelly and from the Shelly to the lights. But some of my lights controlled by i3 units did not respond anymore. And it made me think.

The fact that you can use rest commands to let them function as D2D (Device to Device) is priceless. With D2D there is always a backup as long as the WiFi is up and running. But configuring all your Shelly devices with rest commands can be a tough job. It is hard to track which commands are issued on which unit.

What I would like to happen / to be able to do: Is it possible to edit the rest commands through the api??? In that case it might be possible to make ShellyForHASS synchronise and edit the rest commands from within Home Assistant. It might even be possible to 'translate' basic automations from Home Assistant into rest commands on local units.

Alternative solutions: It would be ideal if there is a way to configure the local rest commands centrally on the Shelly app of on my.shelly.cloud in the same way you can configure actions for the cloud. For example: could there be a checkmark next to an automation in the Shelly app to turn it into a local action. Or have some other way to just list all rest commands on your system without having to edit the individual units. I have not found an alternative like this yet.

I love your work so far! Keep going strong!

kongo09 commented 3 years ago

That sounds like an interesting idea. To me, it sounds like an external application that lists all your devices and lets your "wire" them to HA lights. It would then generate and apply the device settings outside HA and also generate automations inside HA.

The challenges I would see are twofold:

Actually, what you want is to have everything as local as possible. This would introduce a new type of automation to HA that is quite different from the current concept. It would only exist inside the Shelly device and as such outside HA. This feels a bit like a simple Shelly version of node.RED as a plugin, where the plugin then in the background writes the configuration into the devices.

filmgarage commented 3 years ago

I think these challenges are not impossible to overcome.

filmgarage commented 3 years ago

It turns out I can already get this working as I wanted with some simple REST commands...

I implemented the following:

To keep an eye on the status of local io action url's, I use a REST sensor: (assuming I have a Shelly with IP 192.168.1.5 that triggers an event for a second shelly at IP 192.168.1.10 with url: "http://192.168.1.10/relay/0?turn=on")

sensor:
  - platform: rest
    name: "Shelly Sensor Local Command for OUT ON"
    scan_interval: 600
    resource: http://192.168.1.5/settings/actions
    value_template: '{{ value_json.actions["out_on_url"][0]["urls"] }}'

It will poll every 10 minutes if the local URL is active.

I added an automation that will only run if the local command is not active:

automation:
- id: shelly_ha_automation
  alias: Run this only if not run local
  trigger:
  - platform: state
    entity_id: light.shelly1
    from: 'on'
    to: 'off'
  condition:
  - condition: state
    entity_id: sensor.shelly_sensor_local_command_for_out_on
    state: 'off'
  action:
  - service: light.turn_on
    entity_id: light.shelly2

With this setup I can make a collection of automations that will run only if the local commands are disabled. The big advantage is that I will have a full list of automations on my HA server that represent the automations on the shellies. It's also be possible to configure these HA automations with the very same rest commands as on the local units:

rest_command:
  shelly_1_triggers_shelly_2:
    url: "http://192.168.1.10/relay/0?turn=on"

automation:
- id: shelly_ha_automation
  alias: Run this only if not run local
  trigger:
  - platform: state
    entity_id: light.shelly1
    from: 'on'
    to: 'off'
  condition:
  - condition: state
    entity_id: sensor.shelly_sensor_local_command_for_out_on
    state: 'off'
  action:
  - rest_command.shelly_1_triggers_shelly_2

Anyone knows if it is possible to put the rest_command in the automation itself? Without creating a rest_command entity first? That would make it easier to maintain.

I would love to be able to turn the REST sensor into a REST switch so it is possible to enable or disable the local command. In that case I can create a 'master switch' on HA that enables/disables all local URL's on the Shelly and activates/deactivates HA counterparts accordingly. However I can't figure out how to code the body_on and body_off part of the REST switch:

- platform: rest
  name: "Werkkamer Badkamer URL switch"
  scan_interval: 10
  resource: http://192.168.69.228/settings/actions
  body_on: "enabled=true" # ??? this is not working...
  body_off: "enabled=false" # ??? this is not working...
  is_on_template: '{{ value_json.actions["out_on_url"][0]["enabled"] == true }}'
  headers:
    content_type: application/x-www-form-urlencoded

I guess it would be a lot of work to create all these sensors for local action url's. I would certainly appreciate to be able to add the sensors to my shellies. On top of that I would love to be able to edit the urls from within HA. It is possible to do so using http api calls:

http://192.168.1.5/settings/actions?index=0&name=out_on_url&enabled=true&urls[]=http://192.168.1.10/relay/0?turn=on

If I run this command from a web browser it sets the url for the "Output ON URL" But if I run it as a rest_command in HA strangely enough it does not get set...


# this does not run properly:
rest_command:
  shelly_1_triggers_shelly_2:
    url: "http://192.168.1.5/settings/actions?index=0&name=out_on_url&enabled=true&urls[]=http://192.168.1.10/relay/0?turn=on"

Anyway, love to know what others think of this 'solution'. One thing I must say: I am amazed to see how fast my Shellies respond with these local commands. I am sure I will keep my housemates happy if my HA server freezes. I am definitely going to turn all the static HA automations into local commands. Only the "smarter" commands (circadian light, sun angle automation etc. will be run from my server.