Hi, I’m trying to count the number of times my AC is idle in the last hour. Following this post, I’m using a template to create a sensor for this with type: count, but I’m not able to find the sensor’s state in the developer tools. I’m assuming this is because the conditions are never satisfied, so there’s no data for it. I’ve verified that the when the AC is not running, hvac_action is idle, and that {{ now() - timedelta(hours=1) }} does properly produce a timestamp from an hour previous. Is there anything obviously wrong with my template?
Not exactly sure, what your code should do, but I’m quite sure, you’re mixing things up here.
My guess is, you want either a trigger based template sensor, that triggers when your climate is going into idle? Or is it a history stats sensor, like in the linked thread?
As I can’t say what you’re after with that code, I’ll just stick with what you wrote and what I assume.
First of, the history stats sensor is not a template sensor.
So staying with your example, it should look like this:
# NO template
sensor:
- platform: history_stats # the platform is history stats, climate will be the entity to watch
name: "Idle Count One Hour"
entity_id: climate.hallway
state: "idle"
type: count
start: "{{now() - timedelta(hours=1) }}"
end: "{{ now() }}"
Does that, what you want? If not, we can try a template sensor, that just counts. Even some kind of combination could be useful…
History Stats does not accept attribute as a configuration variable. If you want to use it to observe an attribute of an entity you will need to set up a template sensor to capture the attribute in a state so that History Stats can access it.
Thanks for the quick reply! Lol, I am definitely mixing stuff up. Sorry, should have mentioned that I started with history_stats and tried changing it to climate to see if that worked and it obviously did not.
What I’m trying to accomplish is triggering an automation to lower the temperature setting by one degree when when the HVAC system has been idle n times in the last hour and to trigger another automation which raises the temperature if the system has been running for over an hour. The goal with this sensor is to count how many times climate.hallway.hvac_action == "idle" in the past hour. I’ve only just started messing with custom sensors a couple days ago and didn’t realize that there were both “sensors” and “template sensors”. Thanks for pointing that out. Moving the sensor definition from template_sensor to sensor doesn’t seem to have worked, but your answer led me to the history stats docs docs. I’ll read up and try a few things. Below is my current config. Thanks for the help!
History Stats does not accept attribute as a configuration variable.
Literally just read that in the docs and came the same conclusion. Weird that the config didn’t fail validation because of this. I’ll post back with my results after create a template sensor Thanks!
EDIT: I grepped the logs and I turns out I missed the error. You must provide exactly 2 of the following: start, end, duration
Alright, so I made a template sensor for hvac_action, and I’ve verified that its working properly. However, my history_stats sensor is still now showing up in developer tools. I added a couple of time type sensors, as well, which track how long hvac_action is cooling and idle. Neither of those sensors are showing up either. Any ideas as to why these senors seemingly aren’t being created? Could it be something simple, like it not existing until it has an hour of data? There’s nothing in the logs about them. I’ve also forced the AC on and off multiple times in the last hour, so it shouldn’t be that the state hasn’t changed.
template:
- sensor:
name: "Hallway HVAC Action"
state: "{{ state_attr('climate.hallway', 'hvac_action') }}"
sensor:
- platform: history_stats
name: "Idle Count One Hour"
entity_id: sensor.hallway_hvac_action
state: idle
type: count
duration:
hours: 1
- platform: history_stats
name: "On Time One Hour"
entity_id: sensor.hallway_hvac_action
state: cooling
type: time
duration:
hours: 1
- platform: history_stats
name: "Off Time One Hour"
entity_id: sensor.hallway_hvac_action
state: idle
type: time
duration:
hours: 1
Not a problem, it just didn’t really made sense. But we’re all here to help each other! Now that you explained it, I get what you want (I hope )
That raises one question upfront. Are you sure, you want to count the numbers in one hour, or are you more after a general count? Wouldn’t it make more sense, to just count the idle states, and react after a specified value? Let’s say your threshold is 10. If you count for the number within the hour, you might trigger not before say 50 times… If you react on the count itself, you start after exactly ten times, so no need for “the hour”.
I’d go at it a little different, but I’d only use a simple counter. If you need the one hour thing, you could at it to this variant as well.
So, the goal here is to react when my HVAC has been for a long time continuously. The hour duration for the sensor is arbitrary at the moment, as I think it’s actually fine for it run that long. I want to be able to raise the temperature incrementally until it hits a ceiling I set for it. But I also want to lower the temperature incrementally when the system hasn’t been running much within a given time window.
I think I’m leaning more towards using the “On Time One Hour”, type: time senor now that I see what the state looks like. Since it produces a float, I think I’ll be able to accomplish what I want by triggering based on the on that value. So, one automation which triggers when sensor.on_time_one_hour == 1 and then raises the thermostat one degree, and another when sensor.on_time_one_hour < .5 (The .5 threshold is arbitrary here. I need to test this a bit before I’ll find a good value), and lowers the temperature one degree. There will also be a condition that only allows the automation to run when the target temperature is within a specified range (72 - 77 F). I may also have to check when the automation was last run, so I don’t end up raising or lowering the temperature excessively. Thanks for pointing out the input_number thing. Being able to tweak this threshold in the UI will be nice.
Alright, here’s what I came up with. The “Drop Temperature One Degree” automation is tested and working! I’ll update this post tomorrow with how “Bump Temperature One Degree” does. That one is pretty simple. If the HVAC has been running continuously for an hour and the target temperature is less than 77 F, increment the target temperature. The other one is a bit more complicated. It will decrement the target temperature to a floor of 72 when the HVAC has been running less than 30 minutes in the last hour or if the HVAC has run less than 15 minutes in the last hour, and it’s been over an hour since the automation was last run (or has never been run). Since the numeric_state trigger only gets executed when it changes from .5 to .49, the second trigger ensures that the automation doesn’t “stuck.” It should also help with preventing the automation from lowering the temperature too quickly and causing the HVAC to run too much. All the thresholds are arbitrary at this point, so I’ll be tweaking them as I go. Thanks for all the help! I’ve been thinking about setting up this kind of automation for a long time, and I’m glad to have finally figured it out.