Template sensor and use in automation

Hi - first post :slight_smile:

Have HA running for a couple of months and using manual UI automations mostly but works fine.

Have a Devolo / Danfoss room thermostat and its setup and reporting fine, but now want to use it for switching on and off a switch for an electric room heater according to the current temperature and temperature set point of the thermostat.

Can get the current temperature fine (it has its own sensor) but the temperature set point is within the climate entity of ‘climate.study_thermostat’ entity.

State info for the entity:

hvac_modes:
  - heat
min_temp: 7
max_temp: 35
current_temperature: 18.6
temperature: 18
friendly_name: Study Thermostat
supported_features: 1

I was planning to setup a template sensor to call ‘temperature’ so I can use the temperature set point in automations:

study_temperatureSP:
      friendly_name: Study Temperature Set Point
      unit_of_measurement: '°C'
      value_template: "{{ state_attr('climate.study_thermostat', 'temperature') }}"

This works in the template test box, but where do I putt this code to make it a sensor or how do I use this in the automaton UI?

Have tried template in the automation UI but it throws a float @ data error.

Basic question I know!

Hi there, to make a template sensor with this, you need to copy and paste the following in the configuration.yaml, which can be found in config folder.

template:
  - sensor:
      - name: 'Study Temperature Set Point'
        unit_of_measurement: '°C'
        state: "{{ state_attr('climate.study_thermostat', 'temperature') }}"

Now in case of using this for automations, you really dont need to create this sensor but you can use the template itself for that. To help you more with this, could you explain what is it that you are looking for in detail?

If you have trouble editing the configuration.yaml, you need to first install an addon File editor from the addon store. Open it and mostly it will just open the configuration.yaml file. If it is not opened just click on the folder icon on the top left and select configuration.yaml from it.

Many thanks!

I would rather do this in an automation, but can I do this in the UI?

I want to do the following automations:

  1. Switch on heater socket ‘switch.study_heater_socket’ when ‘sensor.study_thermostat_air_temperature’ is lower than the temperature set point in the template above

  2. Switch off heater socket ‘switch.study_heater_socket’ when ‘sensor.study_thermostat_air_temperature’ is higher than the temperature set point in the template above

I have only used the automation UI before.

Hope this is enough info for a suggestion :slight_smile:
Simon

Aren’t the two requirements you described precisely what a thermostat does?

I want to use the Devolo thermostat rather than other sensors and the data is already available.

Based on your reply, I get the impression you may not understand what the Generic Thermostat integration does. Basically, it will do (using your switch and sensor) what you plan to do using automations.

Anyway, you know best so good luck with your automations.

Apologies, not sure how to use Generic Thermostat from the UI.

Any pointers please?

Not coded properly for ages so relying on the UI to get the basics working :slight_smile:

If you review the Generic Thermostat’s documentation, you will see it is configured by adding information to the configuration.yaml file.

FWIW, it’s not “coding”. Think of it as writing a list of ingredients for a recipe or shopping list. The main difference is that the indentation is critical.

Hope you have file editor working and found the configuration.yaml. In your case you can take two approaches.

The first would be to use an automation. It would look like below

alias: Thermostat Automation
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.study_thermostat_air_temperature
  - platform: state
    entity_id: climate.study_thermostat
    attribute: temperature
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('climate.study_thermostat', 'temperature')|float >
              states('sensor.study_thermostat_air_temperature')|float}}
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.study_heater_socket
      - conditions:
          - condition: template
            value_template: >-
              {{ state_attr('climate.study_thermostat', 'temperature')|float <=
              states('sensor.study_thermostat_air_temperature')|float }}
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.study_heater_socket
    default: []

If you have any doubt regarding copying automation yaml, please refer the below gif.

Second would be to make a generic thermostat and an automation. You will have to copy and paste the following to your config to create a thermostat component.

climate:
  - platform: generic_thermostat
    name: Study Thermostat
    heater: switch.study_heater_socket
    target_sensor: sensor.study_thermostat_air_temperature
    min_temp: 15
    max_temp: 21
    target_temp: 17

This would make a new thermostat that would switch on the heater whenever the target sensor temperature is below the target_temp. The target_temp mentioned in above config is relevant only during startup. In our case we have to set this target temperature to the temperature of the template. For this to happen we need to create on more automation like below.

alias: Set Target Temperature
description: ''
mode: single
trigger:
  - platform: state
    entity_id: climate.study_thermostat
    attribute: temperature
condition: []
action:
  - service: climate.set_temperature
    data_template:
      temperature: '{{ state_attr(''climate.study_thermostat'', ''temperature'') |float}}'
    target:
      entity_id: climate.study_thermostat

The climate entity defined by the Generic Thermostat integration shouldn’t have the same name as the the Danfoss thermostat. In your example it is the same and so the last automation becomes self-referential: it is setting the temperature of climate.study_thermostat whenever there’s a temperature change reported by climate.study_thermostat.

You want the automation to detect changes in the target temperature of the Danfoss thermostat and use that value to set the target temperature of the Generic Thermostat. The two climate entities must have unique names.

Many thanks - just what I was looking for :smile:

Never used the automations like this before with the choose option, but think I get it.

Probably because you aren’t familiar with the Generic Thermostat and opted to go with what you know which happens to involve more code yet produces less functionality. :man_shrugging: