I have a sensor, called sensor.wastetime that shows me the upcoming waste collection time as a text value like this: Recycle trash in 0 days Paper trash in 1 day Special waste in 3 days
In order to sent this to some displays in my house, I need to split the information from this text up into 2 seperate helper text sensors: helper.text1 and helper.text2 like this: Paper trash goes to helper.text1 in 1 day goes to helper.text2
and also skip the line that has 0 in it.
So in my example above I would start with Paper trash in 1 day, split that information and also replace the 1 day with tomorrow.
How would you do this? With an automation? With a template?
It assumes that the source info comes from a sensor called sensor.my_waste_collection_sensor, so youāll need to change this. It also assumes your text helpers are called input_text.helper_1 and input_text.helper_2.
I hope Iāve understood your needs correctly. If not, you may get something out of this.
description: ""
mode: single
trigger:
- platform: time
at: "01:00:00"
condition: []
action:
- service: input_text.set_value
data:
value: >-
{% set sensor_text = states('sensor.my_waste_collection_sensor') %} {%
set split_text = sensor_text.split('trash ') %} {% set helper_1_text =
split_text[0] ~ 'trash' %} {{ helper_1_text }}
target:
entity_id: input_text.helper_1
- service: input_text.set_value
data:
value: >-
{% set sensor_text = states('sensor.my_waste_collection_sensor') %} {%
set split_text = sensor_text.split('trash ') %} {% if '0 days' in split_text[1] %}
{% set helper_2_text = 'today' %} {% elif '1 day' in split_text[1] %} {% set
helper_2_text = 'tomorrow' %} {% else %} {% set helper_2_text =
split_text[1] %} {% endif %} {{ helper_2_text }}
target:
entity_id: input_text.helper_2
Thank you so much for your quick replies @templeton_nash and @tom_l ! @templeton_nash solution with the automation template, is exacly what I was looking for and it works great! Learned something new here! Thank you for that!
@tom_l but I am always open to learn different ways and things, so the goal I had was:
receive waste collection times from API through Home Assistant
convert and split those into 2 Helper entities text 1 and text 2
send them on to the KNX BUS as 2 seperate group addresses
- type: latin_1 # string for 16.000 or latin_1 for 16.001
entity_id: input_text.helper_1
address: "3/4/0"
- type: latin_1 # string for 16.000 or latin_1 for 16.001
entity_id: input_text.helper_2
address: "3/4/1"
use those 2 different group address to display the Inofrmation on wall displays throughout the house.
I had to use 2 text fields, as the Information is too long and overflows the current length of the display field. Unfortunately I think you have to split up the text for KNX BUS based displays (if you donāt want to have a rolling text band)
How would you have solved this with template sensor? Would you be able to split the sensor as well over one template sensor? Thank you!
But to understand the differences here for the future: why should I prefer the template script over the automation? The only differences I can think of, is that the automation has a trigger/timer and the sensor template is automatically tied to the state of the source sensor.
My decision to trigger the automation at 1 am each day was a bit arbitrary. I could have set it to trigger when your sensor.my_waste_collection_sensor changes. The template sensor updates automatically when this sensor changes.
The relevant difference, as @tom_l has stated, is that the input helpers provide additional functionality that you do not need and could be counterproductive. That is, they allow the text to be edited through a dashboard.
Thank you @templeton_nash taht makes sense, I will use the template then instead in this case.
One more question about this: how would you skip to the next line in the list of attributes of a sensors entity?
So for example what I try to do:
template:
- sensor:
- name: "Waste collection type"
unique_id: waste_collection_type
state: >
{% set sensor_text = states('sensor.my_waste_collection_sensor') %}
{% if '0 days' in sensor_text %}
## here skip to the second line of the attribute list, the next pickup date
{% set sensor_text = state_attr[1].('sensor.my_waste_collection_sensor') %}
{% else %}
{% set split_text = sensor_text.split('trash ') %}
{% set helper_1_text = split_text[0] ~ 'trash' %}
{% endif %}
{{ helper_1_text }}
Obviously state_attr[1].('sensor.my_waste_collection_sensor') is not working for skipping to the next line of attributes / pickup dates.
Thank you for any hints how to loop through sensor attribute lines!
When you say āskip to the second line of the attribute listā, Iām not sure what you mean. I think Iāll need to see what your sensor actually outputs.
Could you use the Developer Tools > States to produce the yaml output from your sensor and post that here.
"2023-11-15": Paper trash
"2023-11-20": Plastic trash
"2023-11-22": Bio trash
"2023-11-23": Recycle trash
"2023-11-24": Paper trash
"2023-11-29": Bio trash
"2023-12-04": plastic trash
"2023-12-06": Recycle trash
daysTo: 5
attribution: "Last update: 11/10/23 09:57:06"
icon: mdi:numeric-2-box-multiple
friendly_name: Waste collection dates
I am trying to skip the first date if its down to 0 days remaining to already show the next date.
Reason: if you have two dates following directly the next day, it wonāt show the day before on the screen because its still occupied by the actual today/0 day output.
Wow, honestly what a cool solution @templeton_nash ! This also made my initial task of splitting the sensor text so much easier and nicer! Also gave me great ideas for other sensor with listed attributes I have! Super cool, thank you for that idea!
Here is how my final two sensor looks now, one for the time remaining, one for the waste type. And they skip to the next event if its 0 days / today Works really great!
Waste days remaining:
## check if upcoming pickup date is today
{% if (states.sensor.waste_collection_dates.attributes.items() | list)[0][0] == now().date() %}
## get the remaining days of second pickup date as clear number, because first date is today
{% set days_remaining = ((now() - (states.sensor.waste_collection_dates.attributes.items() | list)[1][0]
| as_datetime | as_local ).days | float(0) | abs | round(0)) %}
## check if days_remaining is tomorrow or not
{% if days_remaining == 1 %}
Tomorrow
{% else %}
in {{ days_remaining }} days
{% endif %}
## if date is not today, just use the first attribute of the remaining days as clear number
{% else %}
{% set days_remaining = ((now() - (states.sensor.waste_collection_dates.attributes.items() | list)[0][0]
| as_datetime | as_local ).days | float(0) | abs | round(0)) %}
## check if days_remaining is tomorrow or not
{% if days_remaining == 1 %}
Tomorrow
{% else %}
in {{ days_remaining }} days
{% endif %}
{% endif %}
Waste type:
## check if upcoming pickup date is today
{% if (states.sensor.waste_collection_dates.attributes.items() | list)[0][0] == now().date() %}
## get the waste_type of second pickup date, because first date is today
{% set waste_type = (states.sensor.waste_collection_dates.attributes.items() | list)[1][1] %}
{{ waste_type }}
## if date is not today, just use the first attribute of the waste_type
{% else %}
{% set waste_type = (states.sensor.waste_collection_dates.attributes.items() | list)[0][1] %}
{{ waste_type }}
{% endif %}