Create sensor as substring of another sensors value

Hello all,

So I have a sensor “daylight” which holds the number of hours of daylight (based on sunrise/sunset).

What I am looking to do is create an automation that will trigger when the number of hours change; i.e - when I hit 8 hours of daylight, do something. I am assuming the best way to go about this is to create a new sensor that holds just the hours from my daylight sensor (which holds something like 7:32).

I am guessing this can be done with a template sensor but I cannot figure out how

No need for another sensor. Just use a numeric state trigger.

trigger:
  - platform: numeric_state
    entity_id: sensor.your_daylight_hours_sensor_here
    above: 7.99

OR if you mean the sensor has an attribute that holds the daylight hours:

trigger:
  - platform: numeric_state
    entity_id: sensor.your_sensor_here
    attribute: daylight_hours # or whatever the attribute is called
    above: 7.99

Right, but will that work if the original sensor holds “7:32” (and not 7.32) … the 7:32 is a string i suppose

Ah, I see.

You can use a value template in the numeric state trigger:

trigger:
  - platform: numeric_state
    entity_id: sensor.your_daylight_hours_sensor_here
    value_template: "{{ state.attributes.value.split(':')[0]|float(0) + state.attributes.value.split(':')[1]|float(0)/60 }}"
    above: 7.99

Or if you expect to use this decimalised hours value in more places than just this trigger you can create a template sensor.

template:
  - sensor:
      - name: Daylight Hours
        icon: mdi:sun
        unit_of_measurement: "h"
        state: >
          {% set timesplit = states('sensor.your_daylight_time_sensor_here').split(':') %}
          {{ (timesplit[0]|float(0) + timesplit[1]|float(0)/60)|round(2, default = 'none') }}

Sweet. That works! Thanks. :+1:

Did you use the numeric state trigger or the template sensor?

Because I made a copy/paste error in the template sensor. I have corrected it.

it was:

timesplit[0]|float(0) + timesplit[0]|float(1)/60

But should have been:

timesplit[0]|float(0) + timesplit[1]|float(0)/60