Coverting Conditions YML to Jinja

I am trying to convert some of my conditions that are in an automation trigger, to binary_sensors in my .yaml files.

Is there any documentation on the common conditions parameters and their corresponding jinja logic?

Example:

conditions:
      - condition: time
        after: "04:54:00"
        before: "23:59:59"
        weekday:
          - sun
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat

or,

      - condition: device
        type: is_off
        device_id: d55ba337933e2f4f3a3f0211ce88d25c
        entity_id: switch.attic_fan_power_state
        domain: switch
        for:
          hours: 0
          minutes: 5
          seconds: 0

Straight from conditions in automations to Jinja? I dont think so. But there is alot of Jinja docs around
For your first example something like this would work in the template sensor:

{% set days = [1,2,3,4,5,6,7] %}
{{ 454 < states('sensor.time').replace(':','')|int < 2359 and now().weekday() in days}}

This would result in a true for times between what u posted and those days.

states('sensor.time').replace(':','')|int This just takes the current time like 21:45 and removes the : so that it can be compared as a simple int. Just remember to do the same with the time you code in. So 4:54 becomes 454

that’s still a bit verbose.

{{ today_at("04:54") <= now() <= today_at("23:59:59") }}

don’t really need the days portion.

Just added that option for if he wanted to select days :frowning:
But today_at is something i did not know, it does require the time & date integration tho.

Correction, what petro posted is better and something i will change in my sensors too, best to use built in features then rely on integrations :S

Want to hear something funny, until today i never realised what verbose meant :smiley:
In short it looks like it means: you typed to much stuff, it can be shorter

1 Like

Your second template would simply be

{% set power = expand('switch.attic_fan_power_state') | first %}
{{ power.state == 'off' and (now() - power.last_updated).total_seconds() > 5 * 60 }}
1 Like

today_at does not need the date and time integration. sensor.time comes from that.

today_at was added a while ago, it causes the template to be resolved every minute on the minute. No need for sensor.time (anymore).

1 Like

Thanks all. I can rewrite them as jinja but was hoping that somewhere in the HA docs there was a conversion doc for all the available conditions parameters. You helped me with the trickiest one so thanks!