I’m trying to create a custom component starting from the REST Sensor code but where I add attributes. I’ve read the documentation and development guide (especially https://home-assistant.io/developers/development_states/), but it’s still not clear to me how I add attributes to a component programmatically. Can you tell me which sensor you are using exactly and has attributes in addition to state?
Thank you, @phileep. I analyzed the plant component and I found out how it processes attributes. It seems one needs to write this method:
@property
def state_attributes(self):
"""Return the attributes of the entity.
Provide the parsed JSON data (if any).
"""
return self._attributes
Right now, my component is called jsonrest and I copied it in ~homeassistant/.homeassistant/custom_components/sensor/jsonrest.py
. The complete code is here: http://paste.ubuntu.com/24981070/
The configuration looks like this:
sensor:
- platform: jsonrest
resource: !secret samygo_tv_living_channelinfo
method: GET
name: TV Living ChannelInfo
scan_interval: '00:05'
- platform: template
sensors:
tv_living_source:
value_template: '{{ states.sensor.tv_living_channelinfo.attributes.source }}'
friendly_name: TV Living Source
tv_living_channel_number:
value_template: '{{ states.sensor.tv_living_channelinfo.attributes.channel_number }}'
friendly_name: TV Living Channel Number
And it works. Behold!
I suppose you can only get that level of information because you actually installed SamyGo on your Samsung TV, right?
All I’ve been able to do without going the SamyGo route, was to control it as if it was using a remote control, using this and this Kodi addon, to allow turning it on via HDMI-CEC and the RPi connected to it.
I’d love to have more TV info like you do on Home Assistant, but I will probably not be able without SamyGo, right?
I’ve checked your links, but that remote control uses an API that has been removed from H series onwards unfortunately.
But yes, SamyGo gives you (apart from root) a series of libraries and tools that hook into the normal TV processes and can expose or inject information, or bypass things, so for instance the recordings are saved unencrypted. I’ve only made a simple web api that exposes that functionality over the network. My API currently works until H series, but can be adapted for J/K with a bit of work.
Here is everything you need once you’ve installed SamyGo: http://forum.samygo.tv/viewtopic.php?p=100959#p100959
And here are the Home Assistant bits: https://forum.samygo.tv/viewtopic.php?f=79&t=11882
Thank you for this @mad_ady! Its just what I needed and works great (using with a different device).
With release 0.57 the jsonrest custom component maybe doesn’t work anymore if the state value of the sensor is bigger than 255 char. I used this great custom component to put the values of multiple json values into different template sensors.
Now I need another solution. The API of tankerkoenig.de only allows 1 request until 10 minutes. I need the price from different gas stations. Simple HA RESTful sensors don’t work with this limitation…
I will test it. I didn’t know there was a limit introduced and report back. Although the state size will probably not matter. Because state is retrieved and parsed into attributes in the same function call (in python), and it doesn’t really matter if it’s truncated when stored, because you already have the attributes.
Anyway, I need to refine it and push it upstream. Ideally it needs to be able to handle nested structures and handle 404 errors gracefully.
Thank you for your response. I changed the state allocation to the STATE_ON constant. Now everything works great again on 0.57.1
Any chance this could make it to become an officially supported component? This is an outstanding job!
This is the output I get:
siteCurrentPowerFlow: {
"updateRefreshRate": 3,
"unit": "kW",
"connections": [
{
"from": "GRID",
"to": "Load"
}
],
"GRID": {
"status": "Active",
"currentPower": 3.68
},
"LOAD": {
"status": "Active",
"currentPower": 3.68
},
"PV": {
"status": "Idle",
"currentPower": 0
}
}
friendly_name: SolarEdge jsonrest
any hints how to “extract” che currentPower values (GRID, LOAD and PV) from it?
This contribution is great, but I believe this should be transformed into a general solution, similar to the “template sensor”
The behaviour should be the same for REST, MQTT or Serial (just naming few I have a current use case)
try:
sensor:
- platform: jsonrest
resource: http://your.url/json
name: solaredge_jsonrest
- platform: template
sensors:
grid_status:
value_template: '{{ states.sensor.solaredge_jsonrest.siteCurrentPowerFlow["GRID"].status }}'
Guys, I’m working on merging the jsonrest with the rest sensor and push it upstream. But first I want to do some tests and make sure I’m not breaking anything. I’ll let you know when I make the Push Request. Hopefully it will be done in a few days/weeks
Thanks @sti0, but I get a
ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template grid_status: UndefinedError: 'homeassistant.core.State object' has no attribute 'siteCurrentPowerFlow'
EDIT: here’s working, really thanks for the hint!! (you just missed attributes)
- platform: jsonrest
resource: [redacted]
method: GET
name: SolarEdge jsonrest
scan_interval: 400
- platform: template
sensors:
grid_consumo:
value_template: '{{ states.sensor.solaredge_jsonrest.attributes.siteCurrentPowerFlow["LOAD"].currentPower}}'
grid_produzione:
value_template: '{{ states.sensor.solaredge_jsonrest.attributes.siteCurrentPowerFlow["PV"].currentPower}}'
For anybody interested in SolarEdge, I’ll write a little tutorial and will try to keep it up to date with the mainstream edition of this great piece of code. You can find relevant post here
Thanks so much guys, you rock!
Ok, I’ve made the time to send a PR for this: https://github.com/home-assistant/home-assistant/pull/10483
Follow progress there and let’s hope it gets merged in the next release.
Ok. Did it out of my head. Missed the attributes part. Sorry for that, but finding your own solution is even better
@sti0 I am using this component (jsonrest.py), but unfortunately it doesn’t seem to parse the values that are in an array?
I would like to read the vallues of a device (Youless LS120) that spits out JSON with values that it reads from my smartmeter:
[{"tm":1513196051,"net":-332.767,"pwr": 407,"ts0":1512921868,"cs0": 0.000,"ps0": 0,"p1": 616.196,"p2": 454.733,"n1": 430.932,"n2": 972.764,"gas": 350.759,"gts":1712132100}]
Or in a prettified-view:
[
{
"tm":1513196051,
"net":-332.767,
"pwr":407,
"ts0":1512921868,
"cs0":0.000,
"ps0":0,
"p1":616.196,
"p2":454.733,
"n1":430.932,
"n2":972.764,
"gas":350.759,
"gts":1712132100
}
]
As you can see the values are in a array.
I added the sensor like this:
- platform: jsonrest
resource: http://192.168.6.28/e
name: 'ls120_json'
scan_interval: 10
Unfortunately, the sensor isn’t added. When i use a different URL (which spits out JSON without array-brackets) it works like i would expect. However, that alternative page doesn’t contain all the values i need
Normally i would use something like value_template: '{{ value_json.[0].gas }}'
but i have no idea how to do it with this custom component.
Can you please enlighten me how to get these JSON values into HASS?
You should create a template sensor to read out the values of the jsonrest sensor.
Something like this:
sensor:
- platform: template
sensors:
mygas:
value_template: '{{ states.sensor.ls120_json.attributes.gas }}'
Please check also if the attributes of your sensor displayed correctly at the dev-state view.
I know! But the sensor isn’t even created in Home Assistant after a restart, when using that URL.
When using a different URL (/a) it is, but that one doesn’t contain all the values i need.
The output of that page looks like this:
{"cnt":"-332,318","pwr":287,"lvl":0,"dev":"","det":"","con":"","sts":"","ps0":0,"raw":0}
Or in a prettified-view:
{
"cnt":"-332,318",
"pwr":287,
"lvl":0,
"dev":"",
"det":"",
"con":"",
"sts":"",
"ps0":0,
"raw":0
}
As you can see, the only differences between those outputs (except from the fields) is that the one i want to use, has square brackets around the JSON data. What do i need to alter in the jsonrest.py
so that it can handle the brackets?