RESTful sensor to read data from Thingspeak

Tags: #<Tag:0x00007f739588f198>

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.

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