I have an integration (Theme Park Times) installed which returns queue times for rides in a time format (HH:MM:SS) but I want to be able to sort them. I can use {{ states(’’) }} to give me the absolute value, but I need to do this for probably 20-30 entities. I assume that creating a modified entity based on the original is the way to go, so that I can then use this in dashboards and automations
Is there a way to create the new “corrected” entities in one go or, better still, a more logical way to achieve this, please?
but with that little detail of the problem you’re trying to solve, it’s hard to advise. Give some actual data and information on what you’re trying to get to?
The integration creates an entity for each ride, with a time as the value. For each of these, I need to create a new (template) entity which will have an integer time in minutes (rather than hh:mm:ss).
As you suggest, I could create a template sensor for each separate entity, but that will need 20 or more template entity (I assume) entries; one for each of the originals. What I was hoping for was a way to create each of them with one command or, even better, a more logical way to apprach this.
I want to be able to use the int value in sorts and in automations etc.
You still haven’t said what output you want. If you’re right about the int giving you minutes, put your entities into this list and it’ll give you a list of dictionaries of entity IDs and states from longest to shortest duration.
{% set ride_entities = ['sensor.crush_s_coaster_disneyland_paris',
'sensor.next_ride',
'sensor.last_ride'] %}
{% set ns=namespace(rl=[]) %}
{% for r in ride_entities %}
{% set ns.rl = ns.rl + [{'id': r, 'state': states(r)|int(0)}] %}
{% endfor %}
{{ ns.rl|sort(attribute='state', reverse=True) }}
You can probably get the list without typing them all out by using:
{% set ride_entities = integration_entities('theme_park_times') %}
That’s fantastic. Thank you very much. I’d like to think that this will give me enough to get what I need. I will have a play with this.
The int does indeed give me minutes which is both good and a little frustrating as getting there was easy, but applying it to all of the entities was less so.
How do I get these new values to set as a (new?) set of entities such that I can use them in triggers in an automation or as entries in a entity list in a dashboard?
Can you be more explicit about what the end goal is as well as which integration you are using? The Theme Parks Wait Time integration in HACS doesn’t seem to match the limited details you have provided.
Since states are always strings, you can sort states in “HH:MM:SS” more easily than states converted using the int method mentioned above, because you will just have to perform the int function again when you do your sort. Here’s an analog that you can test in the template editor:
If you want distinct entities, there isn’t really a way around setting up individual template sensors. You cannot use templating to build YAML “live” in your configuration. You canuse the template editor to print something you can copy/paste into your configuration, but it can be as much or more work to do it that way than just doing it manually.
What I am aiming to do is twofold. First, I’d like to use the auto-entities card to display each of the entries which are greater than zero, sorted by longest first.
However, my main requirement is to create an automation which will send me a notification if selected rides drop below a threshold. Ironically the Github page for the integration suggests that this is a helpful use, but I can’t see a simple way to do it. Although, I now wonder whether I could use a template trigger. Ideally I’d have like to have a single automation and a separate list of “selected” rides and a separate trigger value, so that it could be re-used.
I’ve just included the first three entities. To save typing the whole lot, generate the list of entities in the Template Editor for pasting into the YAML view of the automation:
So, I can look for the numeric state, even though the entity is (AIUI) holding the value as text? If it’s that simple, then I can see why my simple misunderstanding is making to too difficult.
When I set up the auto-entities, card, the sort there didn’t work as that was certainly sorting on the text (1:30:00 was less than 5:00).
It depends what these entities are actually holding.
What, for example, do these give in the template sensor (try one at a time if one of them gives an error)?
{{ states('sensor.crush_s_coaster_disneyland_paris') }}
{{ states('sensor.crush_s_coaster_disneyland_paris')|int + 1 }}
x {{ states('sensor.crush_s_coaster_disneyland_paris') }} x
The numeric_state trigger treats the string state as a number. If the state is really the string "01:30:00" it’ll fail, but I don’t believe it is, otherwise this wouldn’t be true:
Yes, that works and it does seem to be treating them as numeric in that context. So, my confusion then must be why, when I use the sensors in the auto-entities card, it shows the values as “text” and then sorts them wrongly.
It looks as if my main misunderstanding is why the values seem to be being treated (displayed?) differently in different contexts.