I have two entities, sensor.offset and sensor.temp from a device . These are polled (value updated by the device) every 10 minutes
I have created a switch heating_state to turn on or off the heating
temp possible ranges from the device can be 20 to 50
offset possible ranges from the device an be zero to 4
I am unsure how to automate the following rules constant automation running permanently and triggered on any value change in temp or offset
Rules:
Whenever temp - offset < 30 then turn off heating
turn on heating whenever offset is zero
introduce some stability mechanism such that heating on/off does not osciliate (on,off,on,off…) for at least 20mins between each state. Note that the sensors are only polled every 10 mins anyway
Off top of my head, you can use “value_template” for your calcs and value evaluation. With this you can create two triggers, add trigger IDs to them, and then a “choose” action in the action section.
You can get more fancy regarding your 3rd point, but a simple one would be adding a “for” to your trigger. In my example, the calculations must evaluate to true (e.g. < 30 ) for at least 5 minutes before triggering.
There are two triggers to address your two options. These triggers are given IDs of “temp-offset” and “offset is 0” (but you can name them whatever you want). In the actions part, you can say essentially: if the automation was triggered by the “temp-offset” trigger, turn the heating off; if the automation was triggered by the “offset is 0” trigger, turn the heating on. Tweak to your preference and final end goal
Trigger IDs coupled with the Choose action is powerful and amazing.
EDIT: I’m not sure if sensor.offset can return a decimal and if you want it to be exactly 0 or not; but something to consider when setting up your automation and using the | int filter as this will round your value
yes the sensors are all to 1 decimal, 19.0, 19.1,19.2…
also
fly in the ointment…
the offset is in the range -4 to +2
i thought it was 0 to 4 where the battle to e subtracted from temp.
so rules would be amended so that offset 0 or +2 turns on the heater,
negative offset values are subtracted and compares to <30 in which case the heater is turned off…
how would I do that?
p.s. still reading up on your initial code to understand it
EDIT: resolved, I have the automation set and running - hopefully through the night for -ve offsets it will keep the heat pump leaving water temperature to >=30C , or , shutdown the pump until the demand offset is positive.
you want to ammend the calc to account for negative values as subtracting a negative would add it, ya?
1 & 3 solution: as I’m not entirely sure the system you’re working with and how exactly you want it to work, so I’m not sure how great of a simple solution this would be: you could +4 to both sides of the equation to remove negative values; the new “absolute offset range” would be 0 - 6 and then you would compare it against “< 34” instead. This preserves the total offset scale (it’s total range is 7 values)…running the offset through a | abs filter I imagine wouldn’t work as a value of +2 or -2 would both just subtract “2”.
2 solution: instead of filtering/piping (in jinja2 templating thats the " | " symbol) as an int, I would filter as a float, this preserves the decimal places
Regarding the understanding of the YAML, it’s likely easier to follow as you make the automation via the GUI as its all doable that way. Choose blocks and trigger IDs are all available via GUI.
Essentially, I’m defining two triggers to trigger the automation
Trigger 1 (temp-offset)
Trigger 2 (offset >=0)
In Action, you can select “Choose” and within that you can create different options. In this case
If the automation was triggered by Trigger 1, then turn OFF the heater
If the automation was triggered by Trigger 2, then turn ON the heater.
You could separate these into two different automations as well, each automation just having the one trigger, this just keeps it within one automation
That is most kind @MikeScottCAD , I have coded that up and extended it to include additional functionality that boosts the heating when the automation starts and the temp is <19.9C.
However can you help as I’m stuck…
This automation only triggers when sensor.room_temp state value changes, and then only when it passes through the 19.9 value from a higher value, because the trigger has below so - I believe - the value must >19.9 then transition to <19.9 for the trigger to fire? This is what I want…
However, I also need a trigger that fires once when the automation is turned on, I believe from reading that there is a homeassistant event that can be used to trigger when the automation is turned on, and each time it is turned on (following a turn-off action). I do this manually (but will automate this later).
So something like the this included in the above , but it doesn’t work, help!
triggers:
- id: Boost
entity_id:
- sensor.room_temp
...
# Trigger each time this automation is started, *not* just when HA starts
- id: Startup # ************* HERE BUT NOT WORKING ****************
homeassistant: # probably wrong
event: # probably wrong
- start # probably wrong
...
conditions: []
actions:
- alias: Temp below day
..
- alias: Started this automation #******* THIS BLOCK WORKS SO OK WITH THIS ********
<<do some actions>>
when you toggle it back on, and if the temp is <19.9, then run your “boost” option
While I’m sure you have some reason to be toggling it on and off, there must be a better way to leave the automation “on” and set it up better to not run when you don’t want it to.
For example, lets say you are toggling the automation OFF at night because you don’t want it to run overnight (I presume the boost is first thing in the morning when you turn the automation back on), instead of toggling the automation, I am more inclined to make a “Times of the Day Sensor” within the Helpers section. With this you can set the time range that the binary sensor will be ON, lets say 9pm to 8am (we’ll call this sensor “Night Time”). Then within your current automation you can
add an extra “And If” condition for the entire automation that looks at the state of binary_sensor.night_time and make sure it is OFF (i.e. daytime) before it runs anything
conditions:
- condition: state
entity_id: binary_sensor.night_time
state: "off"
add an extra trigger, lets say with ID “Boost”, again looking at the state of the aformentioned binary_sensor.night_time, and trigger when it turns OFF (in this example, that will be at 8 am). In your choose action, you will then add an extra condition that looks at sensor.room_temp to ensure when it does turn 8 am in the morning it will also check the temp is <19.9 C before running the action within that option block
triggers:
- trigger: state
entity_id:
- binary_sensor.night_time
from: "off"
to: "on"
id: Boost
#### all the other code in here ######
actions:
- choose:
- conditions:
- condition: trigger
id:
- Boost
- condition: numeric_state
entity_id: sensor.room_temp
below: 19.9
sequence:
- action: climate.turn_on
target:
entity_id: climate.heater
data: {}
Screenshot of UI of this (not the entire automation) - click to reveal
Also based on the general layout of your YAML, I’m not sure if you’re writing this automation via YAML, but I can assure you all of this is achievable, and much easier to do, via the automation Visual Editor (if you are already not doing so). I am only sharing YAML here as its easier to convey the details.
(P.S. if this helps solve your questions, don’t forget to mark your thread as Solved )
Thanks @MikeScottCAD that’s done the job.
Yes, I’m using yaml copied from other examples and trying to tweak. But because of syntax errors then it wont switch to visual editor and aside, I like to see the the full code and learn a bit whilst doing so.
Working now so thanks again.
No worries. Glad I could help. I know I didn’t answer your original question of triggering when you toggle the automation, I just started to assume what you were trying to do and hopefully provided you with a more eloquent solution.
If it really isn’t what you are looking for (that is, you still intend to toggle this automation on/off), let me know if you’d like a bit more help with this
P.S. for what its worth, when you have a moment and isn’t crucial time for the heater running or not, I would try to remake this entire automation from scratch via the Visual Editor. You’ll see just how much easier it is to setup, and future tweaks will be much easier. Also good practice to run it through. That, or just reformat the YAML in time; because if it is formatted properly, you can seemlessly switch between the two modes (unless you’ve done something the visual editor doesn’t support, which I don’t think we’ve done so far)
Thanks for the advice, I actually find it easier in yaml - I’m used to scripting bash in vim so this is childsplay in terms of editing, but it’s a new “language” I’ve only been using two weeks - the lang isnt the challenge , rather learning about the internals of HA and how this is exposed through devices, entities, states, attributes, automations, and how to use this in yaml. Only a few weeks left
If you are up for a challenge Mike, try this one, it’s complex - I may not be explaining very well so all advice and help greatly appreciated. Is it even possible.