Template sensors

Hi, I’m trying to use the platform: template syntax for the first time in my configuration.yaml.

I and using REST to grab data from the pws api at api.weather.com. I have defined the following in my configuration.yaml to grab that data along with the following sensors.

  - platform: rest
    name: pws_report
    json_attributes:
      - observations
    value_template: '{{ value_json["observations"][0]["obsTimeLocal"].title() }}'
    resource: https://api.weather.com/v2/pws/observations/current?stationId=MYSTATIONID&format=json&units=e&apiKey=MYAPIKEY
    scan_interval: 300

  - platform: template
    sensors:
      pws_location:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].neighborhood }}'
      pws_station_id:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].stationID }}'
      pws_type:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].softwareType }}'
      pws_wind_dir:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].winddir }}'
      pws_wind_gust:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].windGust }}'
      pws_wind_speed:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.windSpeed }}'
      pws_humidity:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].humidity }}'
      pws_precip_total:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.precipTotal }}'
      pws_precip_rate:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.precipRate }}'
      pws_temp:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.temp }}'
      pws_solar:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].solarRadiation }}'

My problem is that the sensors don’t seem to get defined as entities when reloading the file or restarting. I’m sure this is something to do with my lack of knowledge, so a pointer in the right direction would be very helpful.

It’s possible the value_template is too complex for HA to work out what entity to watch. Try helping it — example for the first one, repeat for the others if it works:

  - platform: template
    sensors:
      pws_location:
        entity_id: sensor.pws_report
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].neighborhood }}'

If that doesn’t fix it, what does sensor.pws_report show in Developer Tools / States? Is there anything relevant in the HA log (under Configuration in recent versions, otherwise under Developer Tools)?

I think this might be part of the problem. Nothing shows in States.

I’m guessing this is because I can’t find any entities related to pws.

Seems they are not getting created.

Log file shows no issues.

You have restarted HA, haven’t you? Sensor changes still require a restart to get picked up.

Could you post what you get from the resource if you connect in a browser?

Yep, multiple restarts.

If I put the url into a browser it returns the data from the api…

{"observations":[{"stationID":"I90582228","obsTimeUtc":"2020-07-09T09:29:54Z","obsTimeLocal":"2020-07-09 19:29:54","neighborhood":"Macedon","softwareType":"EasyWeatherV1.4.7","country":"AU","solarRadiation":0.0,"lon":144.578995,"realtimeFrequency":null,"epoch":1594286994,"lat":-37.442001,"uv":0.0,"winddir":61,"humidity":89,"qcStatus":1,"imperial":{"temp":48,"heatIndex":48,"dewpt":45,"windChill":46,"windSpeed":5,"windGust":7,"pressure":28.72,"precipRate":0.00,"precipTotal":0.00,"elev":1539}}]}

Can you paste the attributes of sensor.pws_report as seen in the developer tools / states menu?

Hi,

The sensor.pws_report is not showing anywhere. I can’t see it in entities or states and no issues reported in the log.

Here is my configuration.yaml

default_config:

  - platform: rest
    name: pws_report
    json_attributes:
      - observations
    value_template: '{{ value_json["observations"][0]["obsTimeLocal"].title() }}'
    resource: https://api.weather.com/v2/pws/observations/current?stationId=I90582228&format=json&units=e&apiKey=MYAPIKEY
    scan_interval: 300

  - platform: template
    sensors:
      pws_location:
        entity_id: sensor.pws_report
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].neighborhood }}'
  - platform: template
    sensors:
#      pws_location:
#        value_template: '{{ states.sensor.pws_report.attributes.observations[0].neighborhood }}'
      pws_station_id:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].stationID }}'
      pws_type:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].softwareType }}'
      pws_wind_dir:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].winddir }}'
      pws_wind_gust:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].windGust }}'
      pws_wind_speed:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.windSpeed }}'
      pws_humidity:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].humidity }}'
      pws_precip_total:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.precipTotal }}'
      pws_precip_rate:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.precipRate }}'
      pws_temp:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].imperial.temp }}'
      pws_solar:
        value_template: '{{ states.sensor.pws_report.attributes.observations[0].solarRadiation }}'
      solar_angle:
        friendly_name: "Sun Angle"
        unit_of_measurement: '°'
        value_template: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

And the log file.

2020-07-09 19:54:57 ERROR (SyncWorker_14) [homeassistant.util.yaml.loader] mapping values are not allowed here
  in "/config/configuration.yaml", line 6, column 6
2020-07-09 19:55:29 WARNING (MainThread) [homeassistant.components.ring] Error fetching Ring device data: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
2020-07-09 19:56:10 WARNING (SyncWorker_7) [homeassistant.components.sonos.media_player] Could not connect media_player.pool_2: Connection error: HTTPConnectionPool(host='192.168.1.198', port=1400): Read timed out

The ERROR is where I stuck some crap in the configuration.yaml to make sure it was reading the correct file. It is!

See if the sensor shows up when you try this, and just for this test remove json_attributes:

value_template: '{{ value_json["observations"][0]["obsTimeLocal"] }}'

Thanks for the suggestion.

I’ll try that in the morning.

You’re missing the top-level sensor: ahead of the first template sensor description. Like this:

default_config:

sensor:
  - platform: rest
    name: pws_report
    # etc
2 Likes

Thanks, I knew it was going to be something simple!

I can now see all the data in the pws_xxx sensors in entities.

Now I need to figure out how to do something with it. I would like to be able to get alexa to read out the temp. Not sure if that is possible, but I’ll have a go.

1 Like