Minutes data template

Hi all,

I have this automation which do work, to do some action 30 minutes before the sunset:

- alias: Tramonto
  trigger:
    platform: sun
    event: sunset
    offset:
        minutes: -30
  condition:
  ...

I created an input number to change the amount of minutes and i tried the following:

- alias: Tramonto
  trigger:
    platform: sun
    event: sunset
    offset:
        minutes: '{{ states.input_number.offset_tramonto }}'
  condition:
  ...

But this gives me the error

Logger: homeassistant.config
Source: config.py:416 
First occurred: 23:11:46 (1 occurrences) 
Last logged: 23:11:46

Invalid config for [automation]: expected float for dictionary value @ data['offset']['minutes']. Got None. (See /config/configuration.yaml, line 338).

I tried with many castings and templating but nothing. Where is the error?

The sun trigger documentation is here…

Where does it say that the offset can be templated?

You could use a template trigger for this instead.

My bad, that’s right. Thanks.

As suggested, i templated the trigger basing on a binary sensor that i created:

  - platform: template
    sensors:
      offset_tramonto:
        value_template: >-
            {{ (as_timestamp(states.sensor.sunset.state)   - as_timestamp(now()))/60 <  states.input_number.offset_tramonto.state | float}}
     

Notice that i used https://github.com/pnbruckner/ha-sun2 as this allow me to store the sunset of the current day, even it has already elapsed. This allows me to trigger the automations and use the sunset also as a condition, not only as a trigger (eg: i’m home and the sun has already set even since hours)

Then:

- alias: Auto ON al tramonto
  trigger:
  - platform: state
    entity_id: binary_sensor.offset_tramonto
    from: 'off'
    to: 'on'