Logging and creating a temperature graph from SONOFF Device

I have a SONOFF TH16 working in HA, using HACS etc, I can see the status of the switch no issues. But I would like to display the temperature. I can see this by clicking Attributes of the sensor. I use Grapana for my charts, but again that seems to only see the switch state on the TH16.

Any ideas how I can get to the temperature data?

Create a template sensor from the attribute.

Oooo I will have a look, never done anything like that before.

It will end up being something like this:

template:
  - sensor:
      - name: 'Downstairs Set Temperature'
        icon: "mdi:thermometer"
        unit_of_measurement: "°C"
        device_class: temperatrure
        state_class: measurement
        state: "{{ state_attr('climate.downstairs', 'temperature') }}"

I am not using Grafana so no idea bout its capabilities, but other graph tools allow to extract the attrib directly so then you would not need a separate sensor (plotly/apex)

@tom_l question: is there a guideline on when to yes/no add template sensors? In other words, are there downsides to them on e.g. performance, stability, etc. ?

Create them when you need them. e.g. If you only want to display an attribute value in the frontend then the entities card can do that. No template sensor required. However if you want to display a history graph of an attribute you have a couple of choices, (1) create a template sensor and use that in a core history graph card or (2) use a third party card that supports graphing attributes.

Another reason for creating template sensors is to combine more than one state in some way. e.g. The core min_max integration can average a bunch of states but it can’t do a weighted mean, for that you would need a template sensor.

None. Unless you have thousands of them and are trying to run Home Assistant on non recommended hardware like a pi zero.

Hi,
Im still really not getting this, where do i create this template? Sorry

And this is the code I have, but it don work, am I missing something?

template:
  - sensor:
      - name: 'Thermal Store Temp'
        icon: "mdi:thermometer"
        unit_of_measurement: "°C"
        device_class: temperatrure
        state_class: measurement
        state: "{{ state_attr('switch.sonoff_1001031a72', 'temperature') }}"

What errors are you getting?
What are the attributes of switch.sonoff_1001031a72?

Many Thanks, got it sorted in the end :slight_smile:

sensor:
  -   platform: template
      sensors:
       buffer_tank_temp:
        friendly_name: "Thermal Store Buffer Tank"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('switch.sonoff_1001031a72', 'temperature') }}"

This did the trick :slight_smile:

That is the legacy template sensor platform. Not the new template sensor integration. It is also poorly indented.

Sorry I have no idea what I am doing, lol

How should I change it to use the new format?

Like this:

Thank you so much, I am still learning, slow but sure :slight_smile:

Oh, it says Entity Not Available now :frowning:

When I uncomment the new code it gives me an error.

template:
  - sensor:
    - name: house_energy_P1
      state: "{{ states('sensor.owl_intuition_electricity_power_p1') }}" 
      unit_of_measurement: 'kWh'
      state_class: measurement
      device_class: energy
      attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'
          

  - sensor:
    - name: house_energy_P3
      state: "{{ states('sensor.owl_intuition_electricity_power_p3') }}" 
      unit_of_measurement: 'kWh'
      state_class: measurement
      device_class: energy
      attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'
          

#SONOFF Tempweature Data
#  - sensor:
#      - name: 'Thermal Store Temp'
#        icon: "mdi:thermometer"
#        unit_of_measurement: "°C"
#        device_class: temperatrure
#        state_class: measurement
#        state: "{{ state_attr('switch.sonoff_1001031a72', 'temperature') }}"

It goes in your configuration.yaml not your sensors.yaml.

template: is an integration, just like sensor:

Thank you, it is in my config file :slight_smile: