Change Value of an Entity based on Sensor Readings

Hi everyone!

I am trying to create two different Automations that seems like they should be very easy to do, but I lack the understanding of how to implement them in Home Assistant. I am using a “workaround” with many individual automations right now, but this is neither elegant nor practical…

What am I trying to do:

1 (the easy one):
I have a home Ventilation system, controlled by a 0-10V Dimmer switch. This means I can set the Ventilation from 0-100% reliably, smoothly and easily in Lovelace. I even created a “Helper” that turns the Dimmer into a Fan and that also works.
I am controlling this Dimmer based on a couple of CO2 and Humidity Sensors. Currently I use seperate Automations for 20,30,40,50,60,70 and 80% once ONE of the Sensors exceeds a certain value or if ALL of the Sensors drop below a certain threshold. This works, but it’s a whole bunch of Automations and it works in 10% Steps instead of seamlessly.
I would like to “simplfiy” this by making the “Fan” scale seamlessly up when on of the Sensors rises and seamlessly down when they all fall again based on a scale of 20-80% for the Fan and 400-1400ppm of CO2 for the Sensors.
From my Research this would mean using a “PID” integration, but what I have found on them is so far beyond my understanding that I simply don’t know where to start…
Any tips?

2 and this is the tricky one:
I want to charge my Tesla based on excess Solar energy. There are already ideas on how to do that out there and I am using one of them now, but they are very convoluted and have many points of failure since they rely on multiple Sensors.
My idea:

1 Amp of 3 Phase Charging is equal to about 700W of Power. So, once my Power Sensor reads “-700” I could start charging at 1 Amp. That’s the easy part. Now I want the process to always start with 1 Amp, then wait a Minute and keep monitoring the excess. If there is still at least “-700W” of Power available, then it should add another Amp, going to 2 Amps…and so on. If it runs out of excess and instead sees the excess energy going about 0 then it should reverse the Process and start doing “-1” to the Amps until it either reaches 0 (at which Point the Charging is done) or slips below 0 again where it then waits for more or less Power to appear.
My main trouble with this is, that I can’t figure out how to do the “±1” on top of an existing value. I can set the Amps to whatever I want, but I can’t figure out how to “increase” or “decrease” a value…

Thanks for Reading and any Advice you can give!

No PID required. It’s a simple linear equation:

trigger:
  platform: state:
  entity_id: sensor.ppm
  to: # null to, ignores attribute changes and only triggers on state changes. 
action:
  - service: fan.set_percentage
    target: 
      entity_id: fan.ventilation
    data:
      percentage: "{{ ([20, 0.06 * states('sensor.ppm')|float(0) - 4, 80]|sort)[1] }}"

The sort()[1] limits the output to 20-80%.

1 Like

Thank you. I will try to implement this and report back if it works as expected…:+1:

One thing I can see right off the bat is, that I will have to find a new way to make it work with my sensors, because I work with three Sensors (one for each floor) and I need the Automation to always trigger based on the highest of those, which is not always the same one. For now I will base it on the “Lounge” sensor and learn from there.

So…looks like the calculation works perfectly, but for some reason my flush dimmer which is recognised as a fan is not playing nice with this… working on that now…

Edit:

Ok then…got it working for one sensor…will work on adding the others later or in the next days.
I had to go back to using the “light” domain for the flush dimmer because…well…whatever. But it works on the same logic, so no problem. I can’t thank you enough for this, since it makes a long list of convoluted Automations into one simple and compact solution. I will have to see how well it behaves in “real life conditions” but right now everything is looking great.

Yep…was easier than expected with the Min/Max integration.

What is the purpose of this part of the equation:

0.06 * states('sensor.ppm')|float(0) - 4

It converts the CO2 state (400 to 1400ppm) to fan speed (20 to 80%).

0.06 * 400 - 4 = 20
0.06 * 1400 - 4 = 80

1 Like

What do the square brackets contribute? Is it the min/max? And I assume the [1] is for indexing to select the first value (20)?

It is a list. And no, It selects the middle value. The list elements are indexed from 0. [0,1,2]

So the sort function sorts the three numbers in order of increasing magnitude and the [1] picks the middle of the three values.

e.g. to limit a number between 1 and 10:

lets say the number is 5, after the sort:

[1,5,10][1] = 5

If the number is -7, after the sort:

[-7,1,10][1] = 1

If the number is 200, after the sort:

[1,10,200][1] = 10

1 Like