{"id":440,"date":"2018-08-29T07:00:08","date_gmt":"2018-08-29T11:00:08","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=440"},"modified":"2023-02-08T17:04:25","modified_gmt":"2023-02-08T22:04:25","slug":"use-the-rest-component-to-add-a-shelly-switch-to-home-assistant","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/use-the-rest-component-to-add-a-shelly-switch-to-home-assistant\/","title":{"rendered":"Use the REST Component to Add A Shelly Switch To Home Assistant"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/E99-17XyyUg\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>Today on the hookup we\u2019re going to learn how to use the REST api component to add a sensor or switch like the shelly2 to home assistant without the need for a device specific home assistant component.<\/p>\n<p>In my last video about the Shelly 1 I mentioned that the shelly products have a well documented REST API that allows them to be controlled via HTTP commands instead of their app.&nbsp; I sort of mentioned it in passing since the focus of the shelly 1 is that it is so easy to install custom firmware using it\u2019s external gpio headers.<\/p>\n<p>But what if you\u2019re happy with the firmware that it comes with, or what if you want to use some of the other devices from the shelly family?&nbsp; Or maybe you\u2019ve got some other totally unique device in your home that happens to have a REST API?&nbsp; The great news is that home assistant has a very powerful RestAPI component already built in.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/shelly-index.png\"><img fetchpriority=\"high\" decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/shelly-index-300x300.png\" alt=\"\" width=\"300\" height=\"300\"><\/a><\/p>\n<p>In this video I\u2019m going to be setting up the Shelly2, which is a 2 channel relay with power monitoring that is designed to be installed behind a switch plate.&nbsp; It has similar properties to the shelly1, although it doesn\u2019t have a 16A relay, instead it has two 10 amp relays. The other key difference between the shelly1 and the shelly2 is that the shelly2 doesn\u2019t have GPIO pins pre-soldered to be able to easily change the firmware.<\/p>\n<p>But as I mentioned in my last video, the included firmware on the shelly2 has an option to turn off the shelly cloud that in my testing seemed to effectively prevent the shelly from phoning home, and in my tests the shelly sent out only local multicast requests and the occasional request to a time sync server.<\/p>\n<p>So how do you add a shelly device with the factory firmware into home assistant?<\/p>\n<p>Lets start by setting up the shelly2 as a rest switch.&nbsp; According to the shelly rest api documentation the shelly will accept properly formatted http requests to turn the relays on and off.&nbsp; These http requests go to the ip address of the shelly, front slash, relay, front slash, and then the relay number, which would be 0 for relay 1 and 1 for relay 2.&nbsp; These requests need to be formatted as content-type: application\/x-www-form-urlencoded.&nbsp; Basically that means that you could just put the whole url in your browser and turn the relay on and off, but we need to figure out how to format it as a POST request to work with the home assistant REST component.<\/p>\n<p>When I\u2019m testing out my commands for a REST api I like to use the chrome addon \u201cadvanced rest client\u201d or ARC.&nbsp; You can select the type of request and the format of the request from dropdown boxes and it just makes the whole process much simpler. &nbsp;After a bit of fumbling around I was able to figure out the commands I needed to send I was ready to add them to my configuration.yaml file.<\/p>\n<p>Since the shelly2 has two relays I set them up as two different switches, the only difference being the number in the resource URL that corresponds to the specific relay to turn on or off.&nbsp; I\u2019ve posted this yaml in the description and the only thing you\u2019ll need to update to get it working with your shelly is the ip address of the device.&nbsp; If you\u2019re using the shelly1 the only relay that will be available is relay zero.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/ShellyConfig.jpg\"><img decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/ShellyConfig-300x178.jpg\" alt=\"\" width=\"300\" height=\"178\"><\/a><\/p>\n<p>One major difference between MQTT switches and REST switches is that there is about 10 to 15 seconds of lag time from the time the switch is manually toggled to when the state updates in home assistant.&nbsp; This is due to the fact that it needs to send an HTTP GET request to the url to figure out if the switch is on or off, and doing so every second would cause an unnecessarily large amount of extra traffic on your network. There is never any lag in actually turning the switch on and off, only in showing the current state of the switch on the dashboard.&nbsp; However, if this update lag is unacceptable to you you can always add the parameter \u201cscan interval\u201d to your configuration file to tell it how often to issue a GET request to that switch.&nbsp; I wouldn\u2019t recommend going crazy with it, but setting it to a value of 5 seconds or so shouldn\u2019t be a problem since this device is on your local network.<\/p>\n<p>The other cool feature of the shelly2 is that it offers relatively accurate power monitoring.&nbsp; This power monitoring will eventually be available in the custom Tasmota firmware as well, but as of publishing this video that integration is not finished yet.&nbsp; Thankfully, if that\u2019s a feature that you are interested in, it\u2019s not too tough to get it set up in home assistant using the REST sensor component and the factory shelly firmware.<\/p>\n<p>To find out how the data from the shelly is formatted I\u2019m going to use the ARC addon again and send a GET request to my shelly\u2019s IP front slash status.&nbsp; You can see that sending this request results in a significant amount of data returned, but I\u2019m only interested in the power meter which is down here under \u201cmeters\u201d.<\/p>\n<p>When formatting a REST component any JSON response that is returned will be saved by home assistant as the variable value_json.&nbsp; Since I want to know about the power meter I\u2019d format my value template as value_json.meters.&nbsp; If I left it like that it would return all the information inside these curly brackets, but I don\u2019t want all that.&nbsp; I specifically just want to return the value for power, which is the number of watts being consumed by the devices attached to the shelly2.&nbsp; In order to pull out that single value I need put each nested attribute into my value template.&nbsp; Specifically, you can see that the power attribute is first nested within the meters attribute, and then within the zero attribute.&nbsp; The correctly formatted yaml for the power meter sensor is in the description, &nbsp;and again, the only thing you need to change is the IP address of your device and you\u2019ll be up and running.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/sensorYAML.jpg\"><img decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/sensorYAML-300x57.jpg\" alt=\"\" width=\"300\" height=\"57\"><\/a><\/p>\n<p>Now that I\u2019ve got my two switches and my sensor added to my configuration.yaml file I can restart home assistant and test them out.&nbsp; I haven\u2019t wired this shelly2 into a wall switch yet because I\u2019m still actively writing code for it, but I\u2019ve temporarily got it hooked up to an extension cord for testing the power monitoring.&nbsp; Lets first check out switching via home assistant.&nbsp; You can see that switching the shelly2 on and off via the REST component is instant and using a switch an external switch also work and updates in home assistant as expected.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/IMG_3665.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/08\/IMG_3665-169x300.png\" alt=\"\" width=\"169\" height=\"300\"><\/a><\/p>\n<p>You can also see that this card shows the power consumption.&nbsp; In this lamp I\u2019ve got two 60W incandescent bulbs hooked up to relay1, the power monitoring is pretty accurate, not perfect, but still pretty good.&nbsp; Who knows if those bulbs actually consume the amount of power marked on the packaging.<\/p>\n<p>Because I set up this device with the factory firmware through the app I\u2019ll need to use the app to set up any additional options like setting the switchmode.&nbsp; Thankfully the app is well designed and responsive. There\u2019s also a web interface available by simply navigating to the IP address of the device.<\/p>\n<p>Overall, I\u2019m pleased with how well the shelly integrates with home assistant without making any changes.&nbsp; I\u2019ll probably end up flashing Tasmota on all of my shelly devices anyways, but I really wanted to make this video for people who don\u2019t feel the need to put custom firmware on every device in their house.&nbsp; The bin file I posted last week includes profile for the shelly2 that works without issue but doesn\u2019t have power monitoring enabled, it\u2019s a work in progress.<\/p>\n<p>I will make a separate video on flashing the shelly2 with tasmota once the power monitoring gets worked out, but that may be a while.&nbsp; For the time being I\u2019ve included a few pictures in the video description for the pinout on the shelly2 if you\u2019re feeling brave.&nbsp; I was able to get my shelly2 flashed without any issue, but it was FAR from my first soldering project.&nbsp; You\u2019ll need a thin tip on your iron and a steady hand to ensure you don\u2019t mess up any of the surrounding components on the board.<\/p>\n<p>The interest in last weeks video was crazy, and I know the guys at Shelly have been working hard to get the supply chain back up to get the Shelly1 to everyone who was interested.&nbsp; I\u2019ve been told that since the shelly2 has been around longer that the supply is much greater.&nbsp; If you\u2019re interested, you can order the shelly2 from amazon US for $19.90 with prime shipping, and remember that buying through the link in the description helps support my channel without costing you a penny.&nbsp; For my international viewers, I\u2019d recommend ordering directly through the shelly website.<\/p>\n<p>I\u2019m also going to take a quick second thank my patrons for their support, it is honestly super motivating to know that you are enjoying my videos and I hope I can continue to put out content that you enjoy.&nbsp; If you\u2019ve got any questions about anything from this video please leave a comment and I\u2019ll get back to you ASAP.&nbsp; If you\u2019re new to the channel and you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n<p><strong>US Links:<\/strong><br \/>\nShelly 1 Amazon US:<a href=\"https:\/\/amzn.to\/2BzVDYs\"> https:\/\/amzn.to\/2BzVDYs<\/a><br \/>\nShelly 2 Amazon US: <a href=\"https:\/\/amzn.to\/2PqtE0i\">https:\/\/amzn.to\/2PqtE0i<\/a><br \/>\nFTDI Adapter Amazon US: <a href=\"https:\/\/amzn.to\/2PpcoZv\">https:\/\/amzn.to\/2PpcoZv<\/a><br \/>\nJumper Wires Amazon US: <a href=\"https:\/\/amzn.to\/2o0Moa\">https:\/\/amzn.to\/2o0Moa<\/a>1<\/p>\n<p><strong>UK Links:<\/strong><br \/>\nShelly Website: <a href=\"https:\/\/shelly.cloud\/\">https:\/\/shelly.cloud\/<\/a><\/p>\n<p><strong>Files<\/strong><br \/>\nHome Assistant Shelly YAML: <a href=\"https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/Shelly2YAML.yaml\">https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/Shelly2YAML.yaml<\/a><br \/>\nShelly1Tasmota Bin File: <a href=\"https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/shelly1tasmota.bin\">https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/shelly1tasmota.bin<\/a><br \/>\nShelly2 GPIO Pinout: <a href=\"https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/shelly%202%20gpio.jpg\">https:\/\/github.com\/thehookup\/shelly1tasmota\/blob\/master\/shelly%202%20gpio.jpg<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today on the hookup we\u2019re going to learn how to use the REST api component to add a sensor or switch like the shelly2 to home assistant without the need for a device specific home assistant component. In my last video about the Shelly 1 I mentioned that the shelly products have a well documented [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2597,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":5,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":2352,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/440\/revisions\/2352"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2597"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}