Generate Number Based On Existing Entity Attribute

Hey all

Been running HAOS on a Raspberry Pi for about 3 weeks, starting to get the hang of things but stumped on this one. Long explanation, scroll down for TLDR

Issue I’m trying to solve: Hive valves aren’t great at regulating temp, they both open and close too slowly. form what I can gather they work by sensing the current temp, comparing it to the target temp and opening or closing the valve to compensate. The bigger the gap between current and target, the more the valve opens.

For example temp of a rad is say 20c, target temp is 21c. Valve opens so slowly that the temp continues to drop to 19.9, 19.8, until eventually there’s enough temp difference to prompt the valve to open more. Then the rad heats up quickly and over shoots its target to say 21.5c. It then very slowly falls 21.4, 21.3 but again very slowly, because the valve is still a tiny bit open to try and ‘maintain’ the target temp.

Proposed resolution: I want to have 4 variables/attributes. CurrentTemp, TargetTemp (as set on the valve), a TargetLow which would be set to TargetTemp - 2c, and a TargetHigh which would be set to TargetTemp + 2c.

I want to write an automation so that we are still free to set the TargetTemp in the hive app / on the HA dashboard using the Hive integration, but have the following logic:
If CurrentTemp < TargetLow, set TargetTemp to TargetHigh
If CurrentTemp > TargetHigh, set TargetTemp to TargetLow

The aim is basically to force quicker opening and closing of the hive valves to keep it floating close to the target temperature.

I know that CurrentTemp and TargetTemp are attributes which exist in HA already (albeit with different names that I can’t remember right now) - I have no problem finding and utilising those. I need assistance generating the TargetHigh and TargetLow numbers

TLDR - How do I take a numeric attribute of an entity and use it to calculate a different value and store this so I can use it in automation.

Thanks in advance!

template sensors

- sensor:
  - name: Target Low
    state: "{{ states('', 'target_temp') - 2 }}"
    unit_of_measurement: °C
    device_class: temperature

  - name: Target High
    state: "{{ states('', 'target_temp') + 2 }}"
    unit_of_measurement: °C
    device_class: temperature

Thanks for the quick response.

I’ve set the sensors up and they appear, but they just show ‘unavailable’

I’ve tried changing the state of temp in case it needed a change in state to trigger but still no joy.

You have to use the correct attribute and entity_id

Yeah for sure, I wasn’t just using :stuck_out_tongue:

I’m 99% sure I’m using the correct entity_id and attribute. Entity_ID I’ve taken directly from the list of entities and I used the template tool to show all the attributes, see pics below. From what I can tell entity_id is climate.sun_room and attribute is temperature, so that’s what I plugged in to the config code, see pic below:

Template Sensors 2

Update - I needed a state attribute not a state. Correct code is:

- sensor:
  - name: Target Low
    state: "{{ state_attr('', 'temperature')|float - 2 }}"
    unit_of_measurement: °C
    device_class: temperature

Thanks for steering me in the right direction!

Oops typo, but you figured it out