Convert hh:mm to minutes in integer


#1

I have created a template sensor the calculates the delta between the current time and when the bus leaves.
This would give a result such as 0:10:00.

since id like to change the color of the bus icon depending on the time left for the bus to leave I guess I need it in “pure” minutes,

So I can do like this

templates:
icon_color: >
if (state > 6 return ‘rgb(0, 153, 51)’;
if (state =< 6 return ‘rgb(255, 255, 51)’;
if (state < 3 return ‘rgb(255, 51, 51)’;


#2

{{ states.sensor.your_sensor_name.state.split(’:’)[1] | int }}

That should return 10 as an integer. I think…


#3

If that returns the correct value then I think the template should be:

{% if (states.sensor.your_sensor_name.state.split(':')[1] | int )  > 6 %}
  'rgb(0, 153, 51)'
{% elif ((states.sensor.your_sensor_name.state.split(':')[1] | int )  =< 6 ) and ((states.sensor.your_sensor_name.state.split(':')[1] | int )  => 3) %}
  'rgb(255, 255, 51)'
{% elif (states.sensor.your_sensor_name.state.split(':')[1] | int ) < 3 %}
  'rgb(255, 51, 51)'
{% endif %}

#4

Thanks! And if the time is over 1h like 1:05:00?


#5

Do you want to convert that to minutes or separate out the hour and minutes? What would be the goal?

EDIT:

Never mind, I understand what you are asking…

I’ll think about it for a minute…


#6

This will convert the sensor hours to minutes and add the sensor minutes to it to get a total number of minutes:

{% set minutes = (states.sensor.your_sensor_name.state.split(':')[0]|int * 60) + (states.sensor.your_sensor_name.state.split(':')[1] | int) %}
{% if minutes > 6 %}
  rgb(0, 153, 51)
{% elif minutes <= 6  and minutes >= 3 %}
  rgb(255, 255, 51)
{% elif minutes < 3 %}
  rgb(255, 51, 51)
{% endif %}

I also realized I wrote the “=<” & “=>” reversed in my previous post above. It’s correct here.

and if there is a possibility of some other returned value other than hours & minutes then you could add a catchall to the end that will give a default color to handle that situation. If you don’t it will give errors in the log but I don’t think it will break anything tho. But here is that:

{% set minutes = (states.sensor.your_sensor_name.state.split(':')[0]|int * 60) + (states.sensor.your_sensor_name.state.split(':')[1] | int) %}
{% if minutes > 6 %}
  rgb(0, 153, 51)
{% elif minutes <= 6  and minutes >= 3 %}
  rgb(255, 255, 51)
{% elif minutes < 3 %}
  rgb(255, 51, 51)
{% else %}
  rgb(whatever)
{% endif %}

#7

Tried this but its not working

        sensor.jobbet:
      friendly_name: Next Bus
      icon: mdi:bus-side
      templates:
        icon_color: >
          {% set minutes = (states.sensor.leaving_bus_time.state.split(':')[0]|int * 60) + (states.sensor.leaving_bus_time.state.split(':')[1] | int) %}
          {% if minutes > 6 %}
            rgb(0, 153, 51)
          {% elif minutes <= 6  and minutes >= 3 %}
            rgb(255, 255, 51)
          {% elif minutes < 3 %}
            rgb(255, 51, 51)
          {% else %}
            rgb(0, 153, 51)
          {% endif %}

This is done in customizing or can I set the same in the template sensor and set the color of the icon?

The sensor “sensor.jobbet” is the sensor I want to change the icon to using the sensor “sensor.,saving_bus_time” that contains the delta


#8

Sorry, I never looked at the context in which you were wanting to do what you asked. I just concentrated on the templating part and I just assumed that you had the basic functionality all worked out.

As far as I know, you can’t change the icon color in HA without installing a custom-ui.

You can do it using this custom-ui:

and here are the two sections you’ll need to do it:

https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/features.md#icon-color

https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/templates.md

and since I know almost nothing about javascript templates I don’t think I can help any further with this. If I have the time I can try to dig into it and see if I can figure out the template you’ll need but I can’t guarantee any results :slightly_smiling_face:

And lastly, you need to make sure that you properly format your code when posting snippets to the forum.


#9

Seems like I need to separate then things, feels like scripting is not allowed in customizing area?
I got the custom hi installed but it’s still not working.

What if I try in the sensor section? How to template a sensor with a substraction of 2 times?
Eg 23:00 - 22:50 equals 10 as integer?

I could then try using the “10” in the customizing condition section.