nckslater
(Nckslater)
July 16, 2021, 2:38pm
1
I’m wondering if someone can help.
I have just set up some automations to show what electricity tariff is currently running based on time.
In Spain we now have 3 tariffs - Off Peak. Peak and Mid.
These run at different times of the day.
I wanted to show with a picture the current tariff using different images:
This is something probably quite straight forward to do but I’ve not managed it yet.
I have set some times in my config to use as a start but not sure if it will work.
- platform: tod
name: Offpeak
after: '00:00'
before: '08:00'
- platform: tod
name: Mid AM
after: '08:00'
before: '10:00'
- platform: tod
name: Peak AM
after: '10:00'
before: '14:00'
- platform: tod
name: Mid PM
after: '14:00'
before: '18:00'
- platform: tod
name: Peak PM
after: '18:00'
before: '22:00'
- platform: tod
name: Mid PM2
after: '22:00'
before: '00:00'
So I thought to use a Conditional Card but I can’t work it out. My theory is if Mid PM2 is currently ON it will show the relevant pie chart picture… that’s my thinking but maybe there is a better way?
Thanks in advance
tom_l
July 16, 2021, 3:09pm
2
The custom state switch card might be a better fit for your application:
Ever wanted a lovelace card that’s only shown for certain users?
Ever wanted to show a certain lovelace card in one spot if the door is open, and another one otherwise?
Ever wanted to be able to swap out a part of your layout when you press a button?
Ever wanted to do that only on one device, and not on all of them at once?
Ever wanted to show different cards depending on the size of your window, or on wheter you’re viewing it on a touch enabled device or not?
[image]
[image]
State-sw…
Instead of a whole bunch of seperate tod sensors create one sensor with the states Offpeak
, Mid AM
, Peak AM
, etc…
The using the state of that sensor use the state switch card to display the appropriate picture card.
The sensor (will update every minute):
template:
- sensor:
- name: "Tariff in Use"
state: >
{% set t_now = as_timestamp(now()) | timestamp_custom('%H:%M') %}
{% if '00:00' <= t_now < '08:00' %}
Offpeak
{% elif '08:00' <= t_now < '10:00' %}
Mid AM
{% elif '10:00' <= t_now < '14:00' %}
Peak AM
{% elif '14:00' <= t_now < '18:00' %}
Mid PM
{% elif '18:00' <= t_now < '22:00' %}
Peak PM
{% elif '22:00' <= t_now < '00:00' %}
Mid PM2
{% endif %}
The state switch card:
type: custom:state-switch
entity: sensor.tariff_in_use
states:
'Offpeak':
type: picture
imgae: your_offpeak_image.png
'Mid AM':
type: picture
imgae: your_mid_am_image.png
'Peak AM':
type: picture
imgae: your_peak_am_image.png
'Mid PM':
type: picture
imgae: your_mid_pm_image.png
etc...
You can get fancy and use a transition when switching between cards if you like.
nckslater
(Nckslater)
July 16, 2021, 3:13pm
3
@tom_l again to the rescue!
Cool, I’ll give it a go
How can I add to this code if it the weekend then it is se to off peak?
tom_l
July 16, 2021, 3:20pm
4
Like this:
template:
- sensor:
- name: "Tariff in Use"
state: >
{% set t_now = as_timestamp(now()) | timestamp_custom('%H:%M') %}
{% if ('00:00' <= t_now < '08:00') or (now().isoweekday() > 5) %}
Offpeak
{% elif '08:00' <= t_now < '10:00' %}
Mid AM
{% elif '10:00' <= t_now < '14:00' %}
Peak AM
{% elif '14:00' <= t_now < '18:00' %}
Mid PM
{% elif '18:00' <= t_now < '22:00' %}
Peak PM
{% elif '22:00' <= t_now < '00:00' %}
Mid PM2
{% endif %}
nckslater
(Nckslater)
July 20, 2021, 3:56pm
5
tom_l:
custom:state-switch
Thank you. Works perfectly
template:
- sensor:
- name: "Tariff in Use"
state: >
{% set t_now = as_timestamp(now()) | timestamp_custom('%H:%M') %}
{% if ('00:00' <= t_now < '08:00')
Offpeak
{% elif '08:00' <= t_now < '10:00' %}
Mid AM
{% elif '10:00' <= t_now < '14:00' %}
Peak AM
{% elif '14:00' <= t_now < '18:00' %}
Mid PM
{% elif '18:00' <= t_now < '22:00' %}
Peak PM
{% elif '22:00' <= t_now < '00:00' %}
Mid PM2
{% elif (now().isoweekday() > 5) %}
Off Peak WKND
{% endif %}
type: 'custom:state-switch'
entity: sensor.tariff_in_use
states:
Offpeak:
type: picture
image: \local\tariffs\offpeak.jpg
Mid AM:
type: picture
image: \local\tariffs\mid_am.png
Peak AM:
type: picture
image: \local\tariffs\peak_am.png
Peak PM:
type: picture
image: \local\tariffs\peak_pm.png
Mid PM:
type: picture
image: \local\tariffs\mid_pm.jpg
Mid PM2:
type: picture
image: \local\tariffs\mid_pm1.jpg
Off Peak WKND:
type: picture
image: \local\tariffs\off_peak_wknd.png
tom_l
July 20, 2021, 4:22pm
6
You need to move the weekend test in your sensor template to be the first if
statement. Otherwise one of the time tests will be true first, even if it is the weekend.
state: >
{% set t_now = as_timestamp(now()) | timestamp_custom('%H:%M') %}
{% if now().isoweekday() > 5 %}
Off Peak WKND
{% elif ('00:00' <= t_now < '08:00')
Offpeak
{% elif '08:00' <= t_now < '10:00' %}
Mid AM
{% elif '10:00' <= t_now < '14:00' %}
Peak AM
{% elif '14:00' <= t_now < '18:00' %}
Mid PM
{% elif '18:00' <= t_now < '22:00' %}
Peak PM
{% elif '22:00' <= t_now < '00:00' %}
Mid PM2
{% endif %}
1 Like
nckslater
(Nckslater)
July 20, 2021, 4:28pm
7
Yes you are right. It would pick up the time before the weekend :0
Thanks again
1 Like
juaigl
(Juaigl)
July 20, 2021, 4:41pm
8
I don’t know if you know, but there is a custom card for Spain Electricity Prices. Is different than what you want, but I want to tell just in case you don’t know it.
it displais a icon depending on the tarif.