Adjusting Temperature based on state

I am using a sonoff device for switching a lamp and have connected a DHT22 sensor for temp/humidity. Only recently set up and noticed the unit was reporting quite a high temp and when touched it was warm so obviously this was playing a part. I set up the sleep function and that reduced the temp back to normal, and the unit was back to being cold to the touch.

However, when the unit is powered on (e.g. powering a lamp) then the heat does rise again and it effects the Temp / Humidity readings and I want to minimise these incorrect readings.

I know you can use a template sensor to adjust the temp/humidity readings but I only want to do that if the lamp is on - is there a way I can do this?
Need to -5% off humidity, and -1.9 degrees for temp

I doubt that offsetting the values would give you a correct reading for your temperature. I’d rather suggest to place the sensors a few centimetres away from the sonoff or somehow isolate it thermically from the device.

But to answer your actual question you might be looking for something like:
value_template: '{{ (states.sensor.sonoff.state | float) - 2 }}'

as @cgtobi said, you’d use a template sensor with a value_template.

You’d want an if statement checking whether the light is on or off. If the light is on, change the temperature by x amount, if its off, just report the temperature.

Do the same for humidity.

heres an example using a sensor.mysensor and switch.mylight:

value_template:>
  {% set temp = states('sensor.mysensor') | float %}
  {% if is_state('switch.mylight','on') %}
    {{ temp - 1.9 }}
  {% else %}
    {{ temp }}
  {% endif %}

for humidity

value_template:>
  {% set humidity= states_attr('sensor.mysensor', 'humidity') | float %}
  {% if is_state('switch.mylight','on') %}
    {{ humidity * 0.95 }}
  {% else %}
    {{ humidity }}
  {% endif %}
1 Like

Thanks for that - I am going to try mounting the sensor a little differently to hopefully minimise the effect as its obviously not always consistent (e.g. if its 14 degrees in the room when the light is turned on, the difference will not necessarily be the same as if it was 22 degrees in the room) but I can rely on this to minimise any fluctuation once thats done