Sensor to determine if today is a work day

How do I configure a sensor that will tell me if today is a work day?

Currently I have this but it display no before and after work start and end time. I want it to say yes all day unless it’s my day off, which means there will be nothing in the calendar.

       friendly_name: "Is Jamie due at work today"
       value_template: >-
           {% if as_timestamp(now()) | timestamp_custom('%Y-%m-%d') == as_timestamp(states.calendar.jamie_work.attributes.start_time) | timestamp_custom('%Y-%m-%d') %}
           {% else %}
           {% endif %}

This is a classic problem. This template sensor will not update when time changes because the template does not contain any entity that changes with time.

@pnbruckner, Could he manually assign that template sensor an entity_id attribute for a random (but often updated) sensor to get it to work?

Use entity_id: sun.sun

Probably the best way to make it work correctly is to configure, and then use that in the template:

value_template: >
  {% if states('') ==
     as_timestamp(state_attr('calendar.jamie_work', 'start_time'))
     |timestamp_custom('%Y-%m-%d') %}
  {% else %}
  {% endif %}
1 Like

Probably the easiest way is to use the Workday Binary Sensor?

1 Like

I didn’t know this existed and it might be useful for some things but I think I’d rather stick with calendar so I can just delete an event when someone has a day off.

I’ve tried this, so far so good but will give it a few days to make sure.

Thanks :slight_smile:

This is still doing the same thing. After 5pm I switches back to ‘no’

What is state_attr('calendar.jamie_work', 'start_time') after 5pm?

The state is ‘off’

Ok, then it’s doing what it should. What did you expect it to do when that value changed from a valid datetime to 'off'?

Basically I want it to be ‘on’ all day when I have work ,even before and after. Then off all day when I’m not due at work

You’ll have to tell us a bit about how the state of calendar.jamie_work, and especially its attributes, or at least its start_time attribute, changes throughout the day.

From your first post I had guessed that the issue was you hadn’t written the template correctly (i.e., not using an entity that changes with time.) But now apparently the issue has more to do with your calendar.jamie_work entity. You need to describe its behavior.

Here is a screenshot of it currently. It will stay like this all day AFAIK

Then the template sensor should not be changing to No, so you’re probably wrong about that assumption.

Do you know how to query HA’s database to retrieve all the state changes of this entity? If not, then the next best thing is to turn on some debugging and then check home-assistant.log when it doesn’t work as expected. All the details of all state changes will be recorded in the log. To enable the debug mode add this to your config:

  default: info
    homeassistant.core: debug

OK so I think i know what’s going on.

The state of the calendar changes after work because that’s when the event is over. The sensor then changes to ‘no’ because it’s looking at the next event which is my next work day.

So as it stands, this will never work as desired because of this.

Might have to go back to the drawing board on this or look into @sleeepy2’s suggestion.