『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1218|回复: 8

[基础教程] 分享,工作日判定的三种方法

[复制链接]

34

主题

188

帖子

880

积分

高级会员

Rank: 4

积分
880
金钱
692
HASS币
0
发表于 2024-5-13 19:45:24 | 显示全部楼层 |阅读模式
本帖最后由 yun59t 于 2024-5-22 09:42 编辑

本人纯小白,3月底开始摸索ha的使用,从论坛里找了很多大佬做的教程,也分享下自己的使用心得。工作日判定,主要用于联动自动化,比如区分工作日和周末开关灯、电器的时间等。
此次分享的三个有效的方法、一个未实践过的方法,分别是:
1、基于node-red的每年需要手动设置一次节假日的手动修正法。此方法需要每年自己手动修正日期一次,优点是不用去找api,不怕过期。基于liaozhihong修改
2、基于node-red的调取网上免费api获取工作日节假日的api调用法;此方法只要api不挂,就能一直用。基于群里各帖子,通过chatjpt实现。
3、官方有个Workday集成,可以用来判定工作日、节假日。此方法存在一个缺陷,无法将个别周末调成工作日。
4、直接在ha的nfiguration.yaml配置文件中新增一个工作日传感器。这个方法与Workday集成冲突,我实践失败。此方法如果可行,就是纯ha不依赖nodered的方法1。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
一/二、基于node-red的手动修正法、api调用法:①先将flows.json文件导入noed red;
②如果用api法,自己去申请个免费的api;
Snipaste_2024-05-22_08-53-43.png
③部署后,会在ha的node red集成中形成传感器,开启表示工作日,关闭表示休息日(不上班的周末+节假日)。
Snipaste_2024-05-22_09-01-20.png
手动法、api法flows.zip (3.06 KB, 下载次数: 1)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三、官方有个Workday集成,可以用来判定工作日、节假日。
但是,因为中国的节假每年都在调,所以需要手工调整每年的节假,这就出现了两个问题:
①每年都要对节假进行一次手工增加;
②周六、周日,无法添加成工作日。(谁会的,可以在帖子下留言,具体怎么改)
③workday集成链接:https://www.home-assistant.io/integrations/workday
------------------------------------------------------------------------
④使用方法:
1.打开集成链接后,点“ADD INTEGRATION TO MY”图标安装集成,然后在集成中搜索添加;

2.配置集成,配置完提交后,在设置里,会提示哪些有问题,需要删除:
Snipaste_2024-05-13_19-23-09.png Snipaste_2024-05-13_19-40-58.png
3.在开发者工具→服务中,通过官方集成提供的代码,复制进去,可以测试改的日期有没成功。

Snipaste_2024-05-13_13-20-53.png
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
四、在ha的nfiguration.yaml配置文件中新增工作日传感器法:(未经实践,有成功的小伙伴说下)
binary_sensor:
  - platform: workday
    country: CN     # 中国
    workdays: [mon, tue, wed, thu, fri]   # 定义工作日
    excludes: [sat, sun]   # 定义休息日
    add_holidays:           # 添加周一~五可休息的日期(如调休假)=减工作日
      - "2024-06-10"
      - "2024-09-17"
      - "2024-10-01"
      - "2024-10-02"
      - "2024-10-03"
      - "2024-10-04"
      - "2024-10-07"
    remove_holidays:        # 删除周六、周日需要工作的日期=增加工作日
      - "2024-09-14"
      - "2024-09-29"
      - "2024-10-12"


最后,本人目前使用的还是workday集成,使用它的原因是希望哪天,ha能发展到全部依靠自己,不用另外的插件。





回复

使用道具 举报

14

主题

660

帖子

3959

积分

论坛元老

Rank: 8Rank: 8

积分
3959
金钱
3294
HASS币
10
发表于 2024-5-13 21:41:04 | 显示全部楼层
rest 找一个开源的 节假日API
回复

使用道具 举报

3

主题

380

帖子

3325

积分

论坛元老

Rank: 8Rank: 8

积分
3325
金钱
2945
HASS币
0
发表于 2024-5-13 22:51:43 | 显示全部楼层
用chineseholiday
回复

使用道具 举报

34

主题

188

帖子

880

积分

高级会员

Rank: 4

积分
880
金钱
692
HASS币
0
 楼主| 发表于 2024-5-14 09:05:47 | 显示全部楼层
本帖最后由 yun59t 于 2024-5-14 09:24 编辑


                               
登录/注册后可看大图

手动拷进去安装后,无法添加集成,怎么弄
回复

使用道具 举报

3

主题

380

帖子

3325

积分

论坛元老

Rank: 8Rank: 8

积分
3325
金钱
2945
HASS币
0
发表于 2024-5-14 16:59:27 | 显示全部楼层
手动添加配置文件
回复

使用道具 举报

0

主题

3

帖子

36

积分

新手上路

Rank: 1

积分
36
金钱
33
HASS币
0
发表于 2024-6-1 23:08:24 | 显示全部楼层
可以参考  https://bbs.hassbian.com/thread-11829-1-1.html 的方法二(自定义模板方式)
回复

使用道具 举报

20

主题

211

帖子

1535

积分

金牌会员

Rank: 6Rank: 6

积分
1535
金钱
1324
HASS币
10
发表于 2024-6-2 21:28:03 | 显示全部楼层
周末+法定节假日-法定调休。
一年就那么几天,年初国务院办公厅就公布了怎么调整,写一个json格式的文本不就得了。
任何程序判断周六+周日很简单,再加上固定的如元旦五一国庆,清明和端午难一点。
回复

使用道具 举报

20

主题

211

帖子

1535

积分

金牌会员

Rank: 6Rank: 6

积分
1535
金钱
1324
HASS币
10
发表于 2024-6-2 22:12:50 | 显示全部楼层
{"2024":{"01-01":{"holiday":true,"name":"元旦","wage":3,"date":"2024-01-01","rest":1},"01-06":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-01-06"},"01-07":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-01-07"},"01-13":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-01-13"},"01-14":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-01-14"},"01-20":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-01-20"},"01-21":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-01-21"},"01-27":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-01-27"},"01-28":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-01-28"},"02-03":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-02-03"},"02-10":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-02-10"},"02-11":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-02-11"},"02-12":{"holiday":true,"name":"初三","wage":3,"date":"2024-02-12","rest":1},"02-13":{"holiday":true,"name":"初四","wage":2,"date":"2024-02-13","rest":1},"02-14":{"holiday":true,"name":"初五","wage":2,"date":"2024-02-14","rest":1},"02-15":{"holiday":true,"name":"初六","wage":2,"date":"2024-02-15","rest":1},"02-16":{"holiday":true,"name":"初七","wage":2,"date":"2024-02-16","rest":1},"02-17":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-02-17"},"02-24":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-02-24"},"02-25":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-02-25"},"03-02":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-03-02"},"03-03":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-03-03"},"03-09":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-03-09"},"03-10":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-03-10"},"03-16":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-03-16"},"03-17":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-03-17"},"03-23":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-03-23"},"03-24":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-03-24"},"03-30":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-03-30"},"03-31":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-03-31"},"04-04":{"holiday":true,"name":"清明节","wage":3,"date":"2024-04-04","rest":47},"04-05":{"holiday":true,"name":"清明节","wage":2,"date":"2024-04-05","rest":1},"04-06":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-04-06"},"04-13":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-04-13"},"04-14":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-04-14"},"04-20":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-04-20"},"04-21":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-04-21"},"04-27":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-04-27"},"05-01":{"holiday":true,"name":"劳动节","wage":3,"date":"2024-05-01","rest":25},"05-02":{"holiday":true,"name":"劳动节","wage":2,"date":"2024-05-02","rest":1},"05-03":{"holiday":true,"name":"劳动节","wage":3,"date":"2024-05-03","rest":1},"05-04":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-05-04"},"05-05":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-05-05"},"05-12":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-05-12"},"05-18":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-05-18"},"05-19":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-05-19"},"05-25":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-05-25"},"05-26":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-05-26"},"06-01":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-06-01"},"06-02":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-06-02"},"06-08":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-06-08"},"06-09":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-06-09"},"06-10":{"holiday":true,"name":"端午节","wage":3,"date":"2024-06-10","rest":1},"06-15":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-06-15"},"06-16":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-06-16"},"06-22":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-06-22"},"06-23":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-06-23"},"06-29":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-06-29"},"06-30":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-06-30"},"07-06":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-07-06"},"07-07":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-07-07"},"07-13":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-07-13"},"07-14":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-07-14"},"07-20":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-07-20"},"07-21":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-07-21"},"07-27":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-07-27"},"07-28":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-07-28"},"08-03":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-08-03"},"08-04":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-08-04"},"08-10":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-08-10"},"08-11":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-08-11"},"08-17":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-08-17"},"08-18":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-08-18"},"08-24":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-08-24"},"08-25":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-08-25"},"08-31":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-08-31"},"09-01":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-09-01"},"09-07":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-09-07"},"09-08":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-09-08"},"09-15":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-09-15"},"09-16":{"holiday":true,"name":"中秋节","wage":2,"date":"2024-09-16"},"09-17":{"holiday":true,"name":"中秋节","wage":3,"date":"2024-09-17"},"09-21":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-09-21"},"09-22":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-09-22"},"09-28":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-09-28"},"10-01":{"holiday":true,"name":"国庆节","wage":3,"date":"2024-10-01","rest":1},"10-02":{"holiday":true,"name":"国庆节","wage":3,"date":"2024-10-02","rest":1},"10-03":{"holiday":true,"name":"国庆节","wage":3,"date":"2024-10-03","rest":1},"10-04":{"holiday":true,"name":"国庆节","wage":2,"date":"2024-10-04"},"10-05":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-10-05"},"10-06":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-10-06"},"10-07":{"holiday":true,"name":"国庆节","wage":2,"date":"2024-10-07","rest":1},"10-13":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-10-13"},"10-19":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-10-19"},"10-20":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-10-20"},"10-26":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-10-26"},"10-27":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-10-27"},"11-02":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-11-02"},"11-03":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-11-03"},"11-09":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-11-09"},"11-10":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-11-10"},"11-16":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-11-16"},"11-17":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-11-17"},"11-23":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-11-23"},"11-24":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-11-24"},"11-30":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-11-30"},"12-01":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-12-01"},"12-07":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-12-07"},"12-08":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-12-08"},"12-14":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-12-14"},"12-15":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-12-15"},"12-21":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-12-21"},"12-22":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-12-22"},"12-28":{"name":"周六","target":"周末","wage":2,"type":1,"date":"2024-12-28"},"12-29":{"name":"周日","target":"周末","wage":2,"type":1,"date":"2024-12-29"}}}
回复

使用道具 举报

4

主题

327

帖子

2460

积分

金牌会员

Rank: 6Rank: 6

积分
2460
金钱
2133
HASS币
0
发表于 2024-6-3 10:02:11 | 显示全部楼层
有时还会遇上调休,所以我都是用api的。。。不用去管
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 23:07 , Processed in 0.248930 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表