JonasJoKuJonas / homeassistant-WebUntis

Custom component to access data from Web Untis in Home Assistant
https://community.home-assistant.io/t/webuntis-timetable-in-ha/568273
MIT License
46 stars 11 forks source link

Custom Sensor Change Output #105

Closed duckplayz closed 7 months ago

duckplayz commented 7 months ago

Hello, I am pretty new with home assistant and i ran into a problem.

I want to create an Alarm based on the time of the first lesson. For this im using the Sensor template provided in the readme. However the state of the custom sensor is for example "2024-01-31 06:30:00+01:00". I simply want it to be 6:30:00 so i can easily integrate it into a automation. So the sensor should just output for example "6:30:00" or "9:50:00". Is there a way to change that in the Sensor Template?

This is the current state.

- platform: template
  sensors:
    webuntis_wake_up_time:
      entity_id: sensor.webuntis_wakeup_time
      friendly_name: "WebUntis Wake up Time"
      value_template: >
          {% set datetime = states('sensor.MYNAME_next_lesson_to_wake_up') %}
          {% if datetime not in ["unknown", "unavailable", None] %}
            {% set datetime = datetime | as_datetime | as_local %}
            {% set time = datetime.hour|string + ":" + datetime.minute|string %}

            {% if time == "7:55" %}
              {% set weak_up_time = "6:30" %}
            {% elif time == "8:45" %}
              {% set weak_up_time = "7:30" %}
            {% elif time == "9:50" %}
              {% set weak_up_time = "8:15" %}
            {% endif %}

            {{ datetime.replace(hour=weak_up_time.split(":")[0]|int, minute=weak_up_time.split(":")[1]|int) }}
          {% else %}
            {{ None }}
          {% endif %}
JonasJoKuJonas commented 7 months ago

You can use sth like this to trigger the automation

platform: template
value_template: >-
  {{ 0 < as_timestamp(now()) - as_timestamp(states("sensor.webuntis_wake_up_time") | as_datetime | as_local) < 60 }}
duckplayz commented 7 months ago

I simply want to be able to use it in the UI automation creation because I don't really Unterstand the yaml thing. I also plan to use it for some further automations. Isn't there any way to change the sensor yaml so it only shows "6:00:00"?

JonasJoKuJonas commented 7 months ago

you can just return the {{ weak_up_time }} but then it is a text sensor and you canot use it as simple as the datetime sensor. You can also use the yaml in the ui. Just select template as trigger and input

{{ 0 < as_timestamp(now()) - as_timestamp(states("sensor.webuntis_wake_up_time") | as_datetime | as_local) < 60 }}

image

duckplayz commented 7 months ago

So if i use this as a trigger for an automation it will trigger the autmation at the specified time in the sensor template? So if my school starts tomorrow at 7:55 the automation will trigger if its 6:30? Or what do i need to do for that? Sorry if im asking stupid questions but i dont really know much about the yaml thing.

JonasJoKuJonas commented 7 months ago

Exactly, the automation will trigger at 6:30

duckplayz commented 7 months ago

Okay i configured it now and lets hope it works. I simply setup a notification for my phone so i can test if the automation works tomorrow at 6:30! I will report back tomorrow

duckplayz commented 7 months ago

Hello again, It has worked fine today so Thanks for the help!