[SOLVED] Parsing a json value from an existing entity in a template sensor

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.

Then I’m not really setting attributes correctly. I will spy on how other components do it, thanks!

You should be able to look at the states dev tool and tell what state and attributes your sensor has.

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?

a couple of “attribute rich” components:

plant

media player

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!


6 Likes

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

2 Likes

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.

1 Like

Thank you for your response. I changed the state allocation to the STATE_ON constant. Now everything works great again on 0.57.1 :wink:

1 Like

Thanks @sti0!
I’ve incorporated your fix in my github repo.

1 Like

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)

1 Like

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 }}'
1 Like

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 :slight_smile:

2 Likes

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!

1 Like

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.

3 Likes

Ok. Did it out of my head. Missed the attributes part. Sorry for that, but finding your own solution is even better :smiley: