RESTful sensor to read data from Thingspeak

Hi everyone, I need your help for a configuration.
I have a device that sends some data to Thingspeak and I would like to read this data and display it on Home Assistant.

The best way to do this is to create a sensor on the REST platform.

Through the request

https://api.thingspeak.com/channels/xxxxxx/feeds/last.json?api_key=myapikey

I get this result:

{"created_at":"2020-02-17T15:50:54Z","entry_id":71344,"field1":"16.45000","field2":"63.05078","field3":"986.28571","field4":"3.94000"}

field1 temperature, field2 humidity, field3 pressure and field4 battery.

How can I create the sensor? I would like to optimize the process, one request to populate 4 different sensors.

You need to create a rest sensorā€¦ here is my sample code:

  - platform: rest
    resource: https://api.thingspeak.com/channels/915519/fields/1.json?api_key=[REDACTED]&results=1
    name: Water pH
    value_template: '{{ float(value_json["feeds"][0]["field1"]) }}'
    unit_of_measurement: 'pH'

But in this way each request feeds 1 sensor at a time, and to supply the 4 sensors (temp, humidity, pressure and volt) I need to make 4 requests. I wanted to find a more efficient way

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.