Compare 2 sensors + x days

Hi All,

I have 1 sensor. And i want make a trigger based on time of sensor and then 21 days before

‘sensor.fiat_panda_ascription_date’

Output of the sensor is: 2023-06-30

I had this, but its not working. (merge pieces code from forums… )
"{{ ((as_timestamp(strptime(states('sensor.fiat_panda_ascription_date'), '%d %b %Y')) / 86400) | int) == ((as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d')) / 86400) | int) + 21 }}"

Can someone assist me?

It has a date but no time. You want to trigger 21 days before the date but at what time?

Yes just a date. And the trigger must be 21days before the date that sensor have. After that date the sensor change to a new one 2 years in future

That’s understood but there’s no Date Trigger in Home Assistant. What kind of trigger are you planning to use?

For example, we can have a Time Trigger trigger at a specified time and then use a Template Condition to check if the current date is 21 days before the sensor’s date. Or we can use a Template Trigger to trigger at a specific time and 21 days before the sensor’s date. In both cases, the triggers require a time.

The sensor just have a date no time. So then easy is to check every day and condition must be 21 days before the date of sensor. Then it will fire the action.

Check when every day? At 00:00?

The following Template Trigger evaluates the template every minute. When the current date is equal to the sensor’s date less 21 days, it will report true and trigger. This will occur at 00:00 because that’s the starting time of each new day.

  trigger:
    - platform: template
      value_template: >
        {{ now().date() == (states('sensor.fiat_panda_ascription_date') | as_datetime | as_local - timedelta(days=21)).date() }}

If you don’t want it to trigger at 00:00 then you will need to tell me at what time you want it to occur (and then the Template Trigger will be replaced by a Time Trigger and Template Condition).

Very nice. If can also send me with time at 14:00 hour. Then I can learn from it how to do in future myself :slight_smile:

trigger:
  - platform: time
    at: '14:00:00'
condition:
  - condition: template
    value_template: >
      {{ now().date() == (states('sensor.fiat_panda_ascription_date') | as_datetime | as_local - timedelta(days=21)).date() }}

Ohhh offcource this way :slight_smile: I know this way haha stupid me :slight_smile: thank you for assistance!

1 Like

I understand, that this run a 14:00.

How would I compare 2 sensor values and do something if both show different values?

alias: example 
trigger:
  - platform: template
    value_template: "{{ states('sensor.first') != states('sensor.second') }}"
condition: []
action:
  ... etc ...
1 Like