Thanks for the suggestion, but I’m not trying to extract friendly_name. I’m trying to extract data attributes such as source, pvr_status, powerstate that are saved inside an _attributes property in the jsonrest object. Most likely it’s not exposed as the attributes that I can see (like friendly_name).
I’m guessing I shouldn’t call states.XXX, because it’s not a state - it’s something else. But calling it attributes.XXX doesn’t work.
You can get an attribute like this: states.light.master_bath_ledge_accent_level_22_0.attributes.brightness
That’s the brightness attribute for one of my Zwave lights. However you do it, use the template dev tool to experiment.
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
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.
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.
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
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)
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