Noob trying to use REST

I have a few of these wireless sensors

wirelesstag.net/index.html

As per wirelesstag.net/webapp.html it is capable of calling a URL when triggered (e.g. Door opened, Temperature increases etc.)

I would like to monitor these states from Home Assistant

Can anyone give me some direction to achive this?

Thanks

Hello,
I am not familiar with your sensors (i.e. I don’t have them) but I looked them up using the links you posted. It looks like you will be able to use IFTTT and you can have them run API commands.

As far as adding these devices as a component, that is beyond me. You say you would like to monitor them, so to do that you will need HA to communicate with them to record their states I suppose. But if you want them to trigger something like turn on a light or anything tied into your HA system, you can do that by using the API.

As far as direction goes there are two locations you can reference for info on the API.
https://home-assistant.io/developers/rest_api/ and
http://docs.homeassistant.apiary.io/#

If you have specific requests post them and I or someone may be able to assist you. Along the lines of what you mentioned, I have IP cameras that are managed by Blue Iris software. In Blue Iris I can use REST API when the camera is triggered and I do to run on lights. So when certain camera triggers alert based on motion rule, API command is sent to HA and turns on a group of lights. Then i have another command that will turn the lights back off after so many minutes of no motion. It works nicely.

One last thing, you can get an extension for Chrome browser called Postman which is really helpful in testing out REST API to make sure you understand it and practice with it before using it. I recommend that if you are unfamiliar. Use the guidance from the Home Assistant instructions, test with Postman, and then you can have your sensors trigger things.

1 Like

Hi Tobias

Thanks for all your information. I have installed Postman and have been trying some API calls to the HA system just trying to understand how it works.

As for the things I would like to do, there are two in particular that could be useful.

I have these Wireless tags on the front and back door with Hue lamps nearby. I would like to trigger the lamps to come on for a few minutes if the sensor detects door open and it is after sunset (but before sunrise!)
In order to do that, it would need to also understand the current state of the lamp - i.e. don’t turn it on for two minutes if its already on

I can see that IFTTT can detect the movement from the sensors and that the Maker channel can then make a web request to HA. What I don’t understand is how to get this state information stored in HA.

Am I right in expecting that HA will store the current state (and history) of all connected devices?

The other thing I would like to do is to use my OpenWRT router presence detection to identify when we are not home, then if a sensor is triggered, send some sort of alert. But I think I will leave that until I get my head around the first issue!

Cheers

Hi a8ree,
It sounds like you are making progress and doing your research. If you go back and review the documentation on Automation https://home-assistant.io/components/automation/ you will see many topics discussed that handle much of what you are talking about, including IFTTT.

I’d also take a look at:
https://home-assistant.io/blog/2015/09/13/home-assistant-meets-ifttt/
and
home-assistant.io/components/ifttt.manything/

It looks like you can use conditions and rules so that your triggers will only fire if the states are correct.

Also, there are some topics in the mailing list portion of support that I believe pertain to your goals and it sounds like features are on the way that will help. You may want to have a read at some of the discussions there too.

Sorry I am not more help but that’s probably what I would take a good look into and see if you can work it out.

Thanks,
tobias

Hi Tobias

I have been playing around and understanding how this all hangs together…

I used Postman to change the attributes on my Hue lights - state to ‘off’. It was at that time I realised that this was only changing the state in HA and it would not pass onto the Hue itself. Next time HA polled the lights, it corrected the state of the light to ‘on’

I got my sensors set up so that it uses the Maker channel to sent this state change - that works too.

What I next need to work is how to store the state of the sensors. It appears thats what the RESTful Sensor component does??

I added a REST sensor and expected to find a new entity for it when I queried the ‘states’ using the API - but I can’t…where am I going wrong?

Thanks again

States in Home Assistant is the representation of the device in Home Assistant. If you want to control a light, you have to use services.

The REST sensor will query a REST endpoint and show that in the interface. If it cannot reach the REST endpoint when Home Assistant is starting, it will not show up in the UI.

Thanks balloob…learning more as I go along

So, I have worked out that I can get the current states

curl -X POST mytaglist.com/ethClient.asmx/GetTagList -H ‘Content-Type: application/json’ -H ‘Authorization: Bearer d6702ad6-ab3a-4497-8eae-22b6229dfe03’ -d “”

(I know that I have included the auth string, but I can change that later)

I have tried this

sensor:
platform: rest
resource: mytaglist.com/ethClient.asmx/GetTagList
method: POST
name: WirelessTag
content-Type: application/json
authorization: ‘Bearer d6702ad6-ab3a-4497-8eae-22b6229dfe03’

but no success…here is the error

15-12-14 22:42:01 homeassistant.components.sensor: Error while setting up platform rest
Traceback (most recent call last):
File “/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/rest.py”, line 63, in setup_platform
data = loads(response.text)
File “/usr/lib/python3.4/json/init.py”, line 318, in loads
return _default_decoder.decode(s)
File “/usr/lib/python3.4/json/decoder.py”, line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python3.4/json/decoder.py”, line 361, in raw_decode
raise ValueError(errmsg(“Expecting value”, s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py”, line 141, in _setup_platform
self.hass, platform_config, self.add_entities, discovery_info)
File “/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/rest.py”, line 65, in setup_platform
_LOGGER.error(‘No valid JSON in the response in: %s’, data)
UnboundLocalError: local variable ‘data’ referenced before assignment

A lot of keys are not valid in your configuration because they are not supported/implemented. Check the REST sensor docs for a configuration sample.

Oh well, it seems that I am scuppered!

Thanks again for everyone’s input and assistance

Cheers

The workaround for the missing features of the rest sensor is a command sensor with a script.

Don’t give up. I have a lot of these sensors and I love that they support local LAN URL calls. Someone made and app for them in smart things if you want to check out the code and methods they used.

Not sure if you figured it out but I had some success. I followed their instructions to get the OAuth2 code. Then I was able to get data in using this config:

  - platform: rest
    name: 'Wireless 1 Temp'
    resource: https://www.mytaglist.com/ethClient.asmx/GetTagList
    method: POST
    value_template: '{{ value_json.d[0].temperature | round(2) }}'
    unit_of_measurement: °C
    headers:
      Content-Type: application/json
      Authorization: 'Bearer xxxxOAuthCodexxx'

Run that curl command you did above to get the full JSON and then each tag is d[0], d[1]… You should be able to get all data in this way. Great little sensors.

This was very helpful Kevin - thank you! I’m working on making a rest post to a Photon with published web functions (accessible through a restful API). An example of the CURL request is:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'access_token=SECRET&args="1%2C0%2C10"' "https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/"

There are two parameters being passed: access_token and args (string). How should I form a valid rest post in HA given the requirements above?

I think it’s something like what I have below, but I can’t make it over the finish line.

  - platform: rest
    resource: https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/
    method: POST
    name: Particle Devices
    payload: '{ "access_token: SECRET, args: "1,100,30"" }' (Syntax for multiple parameters?)
    value_template: '{{ value_json.state | dictsort }}'   (do I need this?)

Thank you for this. I just purchased some tags, and used this code to pull their values into HA with relative ease. Hardest part was getting the OAuth stuff to work from them.

1 Like

@kevince52 can you please post your configuration and any other code you used to get this working? I am thinking about purchasing one of these tags myself for outside door as well as a motion sensor.

Thanks.

I used the code @kevince52 has above. the trick for me was getting the Authorization key.

I followed the directions here: http://wirelesstag.net/eth/oauth2_apps.html

for the re-direct url, I just put on that wouldn’t work, as all you need is the code that is appended to the URL you are re-directed to after you got o the link in step 1. you then use that code in the curl statement in step 3 to get the Authorization code.

Once I had that, I just put the code @kevince52 has above into my configuration, with the Auth code I got and it worked.

If you use the curl statement from @a8ree above, you can get the full json output from the site. there you can see the names, so I use:

'{{ value_json.d[0].temperature | round(2) }}' For temperature
'{{ value_json.d[0].cap | round(2) }}' for humidity %
'{{ value_json.d[0].lux | round(2) }}' for Light Lux value.

Yep. @tube0013 is same as I did. Just remember that motion is a binary sensor.

Thank you! It looks like these are not water proof… I was looking for a water proof door sensor and maybe a motion sensor too. So, most probably I will keep looking.

@rosahas they have rain proof tags, which can detect motion: not sure if it meets your needs or not:

https://store.wirelesstag.net/pages/support#waterproof

Thanks, will take a look.