brutella / hkknx-public

hkknx is a HomeKit KNX bridge for KNX.
https://hochgatterer.me/hkknx
97 stars 6 forks source link

Support creating automations for KNX #88

Closed brutella closed 3 years ago

brutella commented 3 years ago

For hkknx to be a replacement for a Gira X1 or other servers, it has to offer an automation feature. To get a better understanding of how this should work like, I' asking you (the user) to give me feedback.

This thread is a collection of ideas about what kind of automations should be possible in hkknx.

Some examples are:

Treppenlichtfunktion When motion is detected in the hallway turn on the stair lights. This is done by sending 1 bit (1) to multiple group addresses with a delay.

Schedule irrigation system From Monday to Friday, turn on the irrigation system from 8-9AM. Checking if there is a rain forecast for the next day(s) would be a nice addition.

chef76 commented 3 years ago

If you have questions how one or another can be solved, we can talk.

brutella commented 3 years ago

@chef76 This sounds reasonable. Can you think of any specific automations you want to make? Maybe something that couldn't be done with existing logic server?

chef76 commented 3 years ago

The only thing i couldn't solve until today: Using an incoming SMS to trigger actions in my home, because shortcuts always asks for a manual confirmation. No idea If there's a solution possible around this problem?

Heschbeld commented 3 years ago

A calendar option or integration would be great. I have two Google calendars for public holidays and when I don’t habe to work. Currently I have to deactivate or change my Homekit Automations for my Roller Shutters because I didn’t found a solution to set the time depending on my calendar

brutella commented 3 years ago

@Heschbeld So you have a different set of automations based on when you are at home and when at work?

Heschbeld commented 3 years ago

@brutella Some kind of that. It‘s more based on when I have to work or not. For example on Monday was Easter Monday a public holiday and if I forget to change the time of the HomeKit automation from 6 a.m. to 8 a.m. on such days my Roller Shutters would have been opened too early an woke me up at 6 a.m. In my Gmail Account I have 2 calendars. One, which I subscribed for all public holidays in Germany and a second one, where I enter all my days, when I‘m out of office. It would be nice to have an integration of these calendars. Otherwise a calendar option to set different flags for individual days would be good to have the option to run time based automations at a different time based on this „flag“.

brutella commented 3 years ago

I've made significant progress in the last weeks. I'm planning to add scripts to hkknx. Scripts are little programs written in a Go-like scripting language. They are easy to read and write, but still powerful enough to create complex automations. Everything is explained in the Online Handbook (German only for now).

Heschbeld commented 3 years ago

I've now installed version 2.0 of hkknx and I'm starting to learn the script language to control my Roller Shutters.

First of all I have some points regarding the new Scripts tab. Where can I find the settings for the latitude and longitude for the AtSunrise and AtSunset functions? Also may it be possible to change the behavior of the Save button to stay in the script and not return to the Scripts overview tab?

Regarding my Script itself, I currently have some issues parsing the JSON with the calendar events. I'm not able to parse the second level with the events (items Tag) into another map to finally get the "start" date of the event. With the Unmarshal function I only get the header information structured. Events.txt

I've tried to use a structured type (type Event struct{ ... }) and also the Decoder function (https://golang.org/pkg/encoding/json/#Decoder) but it seems that both are not supported in the Scripts.

How can I get the deeper levels of this JSON into another map or structured type to see if the current day is a public holiday?

brutella commented 3 years ago

First of all I have some points regarding the new Scripts tab. Where can I find the settings for the latitude and longitude for the AtSunrise and AtSunset functions?

It's available under Settings → Location.

Also may it be possible to change the behavior of the Save button to stay in the script and not return to the Scripts overview tab?

This is now fixed in the latest build. Please download it from the website. https://hochgatterer.me/de/hkknx/docs/scripts/#private-beta

Regarding my Script itself, I currently have some issues parsing the JSON with the calendar events. I'm not able to parse the second level with the events (items Tag) into another map to finally get the "start" date of the event. With the Unmarshal function I only get the header information structured.

I've tried to use a structured type (type Event struct{ ... }) and also the Decoder function (https://golang.org/pkg/encoding/json/#Decoder) but it seems that both are not supported in the Scripts.

How can I get the deeper levels of this JSON into another map or structured type to see if the current day is a public holiday?

I would suggest to use a map to access the JSON data instead of a custom type.

var hkknx = import("hkknx")
var time = import("time")
var json = import("encoding/json")

str = `{
 "kind": "calendar#events",
 "etag": "\"p32887lm8qn5v00g\"",
 "summary": "Feiertage - Baden-Württemberg",
 "description": "Hier findest Du alle Feiertage für das Bundesland Baden-Württemberg.\n\nDie einzelnen Bundesländer legen die Feiertage für das jeweilige Land fest, unabhängig davon, ob sie bundesweit gelten oder nur landesbezogen. Ausschließlich der Tag der Deutschen Einheit wurde im Rahmen deines Staatsvertrags durch den Bund beschlossen. Feiertage sind „Tage der Arbeitsruhe und der seelischen Erhebung“ und zusammen mit allen Sonntagen in Deutschland verfassungsverankert garantiert.",
 "updated": "2021-05-15T12:17:45.882Z",
 "timeZone": "Europe/Berlin",
 "accessRole": "reader",
 "defaultReminders": [],
 "nextSyncToken": "CJCD1sjVy_ACEAAYASDoi-axAQ==",
 "items": [
  {
   "kind": "calendar#event",
   "etag": "\"3148890405428000\"",
   "id": "_6li68e1hccqjip1mc4pjeg1m6gn66obcdtr6u",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=XzZsaTY4ZTFoY2NxamlwMW1jNHBqZWcxbTZnbjY2b2JjZHRyNnUgazc1aHUwYjhwYTB0NmowN2gyZjl2N2k3NTUzZnRmb29AaQ",
   "created": "2019-11-22T17:35:21.000Z",
   "updated": "2019-11-22T17:53:22.714Z",
   "summary": "Tag der Arbeit",
   "description": "Dieser Terminservice wird präsentiert von calovo.de - kostenlos eigenen Kalender anlegen und mit der Welt teilen: http://bit.ly/calovo_kostenlos_starten\n\nGesetzlicher Feiertag 2021 im deutschen Bundesland Baden-Württemberg\n\n\nAnbieter-Impressum: https://calovo.de/impressum\n\n\ncalfeed löschen? Anleitung: bit.ly/calfeed_loeschen",
   "location": "calovo.de | Baden-Württemberg",
   "creator": {
    "email": "k75hu0b8pa0t6j07h2f9v7i7553ftfoo@import.calendar.google.com",
    "displayName": "Feiertage - Baden-Württemberg",
    "self": true
   },
   "organizer": {
    "email": "k75hu0b8pa0t6j07h2f9v7i7553ftfoo@import.calendar.google.com",
    "displayName": "Feiertage - Baden-Württemberg",
    "self": true
   },
   "start": {
    "date": "2021-05-01"
   },
   "end": {
    "date": "2021-05-02"
   },
   "transparency": "transparent",
   "iCalUID": "5dd81c59d6a37@64.calovo",
   "sequence": 0,
   "eventType": "default"
  },
  {
   "kind": "calendar#event",
   "etag": "\"3148890405428000\"",
   "id": "_6li68e1hccqjip9gcksjgg1m6gn66obcdtr6u",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=XzZsaTY4ZTFoY2NxamlwOWdja3NqZ2cxbTZnbjY2b2JjZHRyNnUgazc1aHUwYjhwYTB0NmowN2gyZjl2N2k3NTUzZnRmb29AaQ",
   "created": "2019-11-22T17:35:21.000Z",
   "updated": "2019-11-22T17:53:22.714Z",
   "summary": "Christi Himmelfahrt",
   "description": "Dieser Terminservice wird präsentiert von calovo.de - kostenlos eigenen Kalender anlegen und mit der Welt teilen: http://bit.ly/calovo_kostenlos_starten\n\nGesetzlicher Feiertag 2021 im deutschen Bundesland Baden-Württemberg\n\n\nAnbieter-Impressum: https://calovo.de/impressum\n\n\ncalfeed löschen? Anleitung: bit.ly/calfeed_loeschen",
   "location": "calovo.de | Baden-Württemberg",
   "creator": {
    "email": "k75hu0b8pa0t6j07h2f9v7i7553ftfoo@import.calendar.google.com",
    "displayName": "Feiertage - Baden-Württemberg",
    "self": true
   },
   "organizer": {
    "email": "k75hu0b8pa0t6j07h2f9v7i7553ftfoo@import.calendar.google.com",
    "displayName": "Feiertage - Baden-Württemberg",
    "self": true
   },
   "start": {
    "date": "2021-05-13"
   },
   "end": {
    "date": "2021-05-14"
   },
   "transparency": "transparent",
   "iCalUID": "5dd81c59e0e98@64.calovo",
   "sequence": 0,
   "eventType": "default"
  }
 ]
}`

var data = make(map[string]interface)
err = json.Unmarshal(str, &data)
if err != nil {
    println(err)
    return
}

layout = "2006-01-02"
for item in data["items"] {
    start, _ = time.Parse(layout, item["start"]["date"])
    end, _ = time.Parse(layout, item["end"]["date"])
    printf("%s \t\t %s \t %s \n", item["summary"], start.Format(time.RFC822), end.Format(time.RFC822))
}

Hope this helps.

Heschbeld commented 3 years ago

It's available under Settings → Location.

I've now found it. I thought I checked the whole settings page.

I would suggest to use a map to access the JSON data instead of a custom type.

Your example helped me a lot. I didn't know that ist possible to access the data with double the square brackets I'm now currently checking, if time zones are relevant for my scenario or not.

The last step then will be to get familiar with the channels and select to make sure to not do to many API calls, because the number of free of charge calls is limited per day.

Do you think it may be possible and a good idea to integrate the possibility to activate and deactivate or even run these scripts in your Home+ App (Manually and with Automation)? I for example have no temperature in my KNX system and I'm using Eve Thermo to control and measure the temperature in my rooms. My idea is to control the blinds completely with hkknx and trigger a script to close the blinds, if the room temperature is getting to high.

brutella commented 3 years ago

I've just updated the beta version 2.0 with some new script functions.

  1. You can now send notifications to Apple devices using hkknx.SendNotification()https://hochgatterer.me/de/hkknx/docs/scripts/push-notification/
  2. You can download weather data using hkknx.GetCurrentWeather(), hkknx.GetHourlyWeather() and hkknx.GetDailyWeather()
brutella commented 3 years ago

Version 2.0 is now available as pre-release. → https://github.com/brutella/hkknx-public/releases/tag/2.0.0

brutella commented 3 years ago

@nanosonde Please create a separate issue for this