I recently created a binary sensor for Home Assistant that lets me know if it’s off-peak electricity hours or not. It’s a pretty simple piece of code, but it’s been really helpful for me in terms of managing my energy usage.
Here’s how it works: the sensor checks the current date and time, and then compares it to the off-peak hours for my area. If it’s currently off-peak, the sensor will be “on” and I’ll know that I can use my appliances without worrying about high energy costs. If it’s not off-peak, the sensor will be “off” and I’ll know to be more mindful of my energy usage.
One of the best things about this binary sensor is that it’s really useful for automations. For example, if I have a smart washing machine that I can control over my home network, I can set up an automation so that the washing machine only runs during off-peak hours. This way, I can save money on my energy bills and make sure that I’m not using too much energy during peak hours.
The same is true for other energy-hungry devices, like an electric car. I can set up an automation to make sure that I only charge my car during off-peak hours, which will save me money and help me be more energy efficient.
Overall, this binary sensor has been a great addition to my Home Assistant setup, and I highly recommend it to anyone who wants to save money on their energy bills and be more environmentally friendly.
Here’s the code for the sensor:
template:
- binary_sensor:
- name: "Off-peak"
icon: "mdi:lightning-bolt"
state: >
{% if ( now().weekday() in [0,1,2,3,4] ) %}
{{ today_at('00:00') <= now() < today_at('07:00') }}
{% elif ( now().weekday() == 5 ) %}
{# Summer time first, then winter. #}
{% if ( now().timetuple().tm_isdst > 0 ) %}
{{ ( today_at('00:00') <= now() < today_at('09:00') ) or ( today_at('14:00') <= now() < today_at('20:00') ) or ( today_at('22:00') <= now() <= today_at('23:59:59') ) }}
{% else %}
{{ ( today_at('00:00') <= now() < today_at('09:30') ) or ( today_at('13:00') <= now() < today_at('18:30') ) or ( today_at('22:00') <= now() <= today_at('23:59:59') ) }}
{% endif %}
{% else %}
{{ true }}
{% endif %}
To add the binary sensor code to your Home Assistant configuration, copy the code from the blog post and paste it into your configuration.yaml file. Be sure to save the file after making the changes, and then restart Home Assistant to apply the changes.