RESTful sensor to read data from Thingspeak

I think your best bet is to createe the single sensor with multiple attributes, you will then need a template sensor to pull out the individual attributes into their standalone sensors

Unfortunately I am still a novice with HA and the template are still a difficult topic for me :frowning:

here is a sample from another sensor I creted using this method. Battery in the below was an attribute of the mobimeds device tracker sensor

- platform: template
  sensors:
    mobimeds_battery:
      friendly_name: 'MobiMeds Battery'
      value_template: '{{ states.device_tracker.mobimeds.attributes.battery | round }}'
      unit_of_measurement: "%"
      icon_template: >-
       {% set battery = states('sensor.mobimeds_battery')|int('unknown') %}
       {% set battery_round = (battery|int / 10)|int * 10 %}
       {% if battery == 'unknown' %}
         mdi:battery-unknown
       {% else %}
         {% if battery_round >= 100 %}
           mdi:battery
         {% elif battery_round > 0 %}
           mdi:battery-{{ battery_round }}
         {% else %}
           mdi:battery-alert
         {% endif %}
       {% endif %}

I tried to adapt the example to my situation:

- platform: rest
  name: Thingspeak
  json_attributes:
    - field1
    - field2
    - field3
    - field4 
  resource: https://api.thingspeak.com/channels/xxxxxx/feeds/last.json?api_key=myapikey
  value_template: I have no idea what to put on
- platform: template
  sensors:
    temperature:
      friendly_name: 'Temperatura'
      value_template: '{{ states.sensor.thingspeak.attributes.field1 | round }}
      device_class: temperature
      unit_of_measurement: '°C'
    other sensor:
      etc

try this (or something like this)

value_template: '{{ states.sensor.thingspeak.attributes.field1 | round }}'

but first make sure your thingspeak sensor is working and that the attributes are coming through… have a look in >developer tools > states

Thanks for your help! :slight_smile:

The template you suggest refers to the platform: template, but what is the value_template for the platform: rest?

you are going to need to play around with this line in the thingspeak rest sensor

value_template: '{{ float(value_json["feeds"][0]["field1"]) }}'

but I am not sure how to do this or even if it is possible… I would suggest reading up on the docs and then using the template tool in developer tools section:

https://your-domain:8123/developer-tools/template

Solved!!

  - platform: rest
    name: Sensore Thingspeak
    json_attributes:
      - field1
      - field2
      - field3
      - field4
    resource: https://api.thingspeak.com/channels/xxxxxx/feeds/last.json?api_key=myapikey
    value_template: '{{ value_json.created_at }}'
  - platform: template
    sensors:
      temperatura:
        friendly_name: 'Temperatura balcone cucina'
        value_template: '{{ state_attr("sensor.sensore_thingspeak", "field1") | round(1)}}'
        device_class: temperature
        unit_of_measurement: '°C'
     other sensor.....

Thanks to the guide and tips here on the forum I created the sensor. A single http request feeds a sensor (last reading time) with 4 arguments (temp, humidity, etc.). With a templete I take the data from the arguments and create individual sensors.

2 Likes

Im working on this same integration right now, EC, pH and Temperature. when i use the request https://api.thingspeak.com/channels/xxxxxx/feeds/last.json?api_key=myapikey i get {“created_at”:“2020-06-10T00:54:13Z”,“entry_id”:299,“field1”:“7.24”,“field2”:“294”,“field3”:“20.79”}.

This is my code:

sensor:
  - platform: rest
    name: Sensor Thingspeak
    json_attributes:
      - field1
      - field2
      - field3
    resource: https://api.thingspeak.com/channels/1078762/feeds/last.json?api_key=myapikey
    value_template: '{{ value_json.created_at }}'
  - platform: template
    sensors:
     temperature:
        friendly_name: 'Temp(°C)'
        value_template: '{{ state_attr("sensor.sensore_thingspeak", "field3") | round(1)}}'
        device_class: temperature
        unit_of_measurement: '°C'
     pH:
        friendly_name: 'pH'
        value_template: '{{ state_attr("sensor.sensore_thingspeak", "field1") | round(1)}}'
        device_class: pH
        unit_of_measurement: 'pH'
     EC:
        friendly_name: 'EC (µS/cm)'
        value_template: '{{ state_attr("sensor.sensore_thingspeak", "field2") | round(1)}}'
        device_class: EC
        unit_of_measurement: 'μS/cm'

How can i call this sensor in lovelace?

Did you find a way to do it?

I’m getting this error, any ideias?

Translation Error: The intl string context variable “Level” was not provided to the string “Detalhes do log ({Level})”
Logger: homeassistant.config
Source: config.py:816
First occurred: 19:07:44 (1 occurrences)
Last logged: 19:07:44

Invalid config for [sensor.template]: value is not allowed for dictionary value @ data[‘sensors’][‘ec’][‘device_class’]. Got ‘EC’ value is not allowed for dictionary value @ data[‘sensors’][‘ph’][‘device_class’]. Got ‘pH’. (See /config/sensor.yaml, line 8). Please check the docs at https://www.home-assistant.io/integrations/template

I ended up ditching the thingspeak all together and used mqtt for my sensors instead

Hi @sp3cialck

It’s because of ‘device_class’ name. They must be respect specific name, for ex. Humidity, pressure, temperature, illuminance… etc

I’m trying hard to make this work, but all I get is the time when set up as above. Perhaps I don’t understand the “Sensor.sensore_thingspeak” part of the Value_Template. Where does sensore_thingspeak come from?

Anyone got recent working example? Or an example of code for MQTT subscribe to Thingspeak?

I’m very interested in how you got MQTT to work here. Got any recent examples with MQTTv3 that thingspeak is using?

Hi Albert! I am also looking to accept data from an API key and I have four fields of data. I’ve tried recreating your code but it seems that only Sensor_ThingSpeak shows a change in time. The other sensors are not accepting any information. Can you maybe spot my mistake?

Appreciate anyone’s help!

When you get into developer tools → states, can u see all the necessary fields for the Sensor Thingsspeak under attributes? For me i can only see the last updated time and no attributes.

Hi all -

I just wanted to chime in here. I just received my Hydroponics Kit from ThingSpeak and had configured it all to their specifications and calibrated everything then looked to see about implementing it into Home Assistant. I saw the MQTT topic but since I had already calibrated I didn’t want to re-wipe the chip and waste all of that so I was determined to get the API working and that led me here.

I usually don’t post questions and will dig around tirelessly until I find an answer so this topic had everything I needed except a lot of the answers seemed to have a lot of assumptions and I couldn’t put two and two together until I came to @Segdop’s post and saw the full context of the yaml needed. I was able to use what he put with some slight modifications as there was a couple of issues with it.

This is what I was able to put into my configuration.yaml where it worked beautifully for me. All you need to do is put your channel id and api key in the right spots:

sensor:
  - platform: rest
    name: Hydroponic Kit
    json_attributes:
      - field1
      - field2
      - field3
    resource: https://api.thingspeak.com/channels/[CHANNEL ID]/feeds/last.json?api_key=[API KEY]
    value_template: '{{ value_json.created_at }}'
  - platform: template
    sensors:
     hydroponic_kit_temperature:
        friendly_name: 'Temp'
        value_template: '{{ state_attr("sensor.hydroponic_kit", "field3") }}'
        unit_of_measurement: '°C'
     hydroponic_kit_ph:
        friendly_name: 'pH'
        value_template: '{{ state_attr("sensor.hydroponic_kit", "field1") }}'
        unit_of_measurement: 'pH'
     hydroponic_kit_ec:
        friendly_name: 'EC'
        value_template: '{{ state_attr("sensor.hydroponic_kit", "field2") }}'
        unit_of_measurement: 'µS/cm'

My tap water readings which yield the expected results:

image

Hopefully this helps someone.

2 Likes

Perfect I can now pull the data from my esp8266 weather station

sensor:
  - platform: rest
    name: Waynes Weather
    json_attributes:
      - field1
      - field2
      - field3
      - field4
    resource: https://api.thingspeak.com/channels/[CHANNEL ID]/feeds/last.json?api_key= [API KEY]
    value_template: '{{ value_json.created_at }}'
  - platform: template
    sensors:
     waynes_weather_temperature:
        friendly_name: 'Temp'
        value_template: '{{ state_attr("sensor.waynes_weather", "field1") }}'
        unit_of_measurement: '°C'
     waynes_weather_humidity:
        friendly_name: 'Rh'
        value_template: '{{ state_attr("sensor.waynes_weather", "field2") }}'
        unit_of_measurement: 'rH'
     waynes_weather_light:
        friendly_name: 'Lux'
        value_template: '{{ state_attr("sensor.waynes_weather", "field3") }}'
        unit_of_measurement: 'lx'
     waynes_weather_pressure:
        friendly_name: 'Hg'
        value_template: '{{ state_attr("sensor.waynes_weather", "field4") }}'
        unit_of_measurement: 'hg'

image

When I paste the code into my yaml I get problems with other stuff