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

 找回密码
 立即注册
查看: 3647|回复: 14

[基础教程] 官方节假日支持中国了做一个节日倒计时的功能

[复制链接]

14

主题

123

帖子

2028

积分

金牌会员

Rank: 6Rank: 6

积分
2028
金钱
1900
HASS币
40
发表于 2024-1-24 09:42:27 | 显示全部楼层 |阅读模式
老规矩,先看效果图:
Snipaste_2024-01-24_09-21-12.png
前提条件:
homeassistant端
1、配置----设备与服务----添加集成
2、搜索 holiday
3、选择 中国  添加
nodered端
[{"id":"75d1cd431666fc49","type":"api-current-state","z":"6ce0f0465744cfa4","name":"日历","server":"9b9f41a8.34d99","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"calendar.zhong_guo","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":250,"y":1580,"wires":[["6b6e012ebe2526a7"]]},{"id":"093120e87262ad8d","type":"inject","z":"6ce0f0465744cfa4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":1580,"wires":[["75d1cd431666fc49"]]},{"id":"6b6e012ebe2526a7","type":"function","z":"6ce0f0465744cfa4","name":"function 2","func":"// 获取当前日期并转换为毫秒数\nvar now = new Date();\nvar now_ms = now.getTime();\n\n// 获取实体属性日期并转换为毫秒数\n// 假设实体属性日期是msg.payload\nvar entity_date = new Date(msg.data.attributes.start_time);\nvar entity_date_ms = entity_date.getTime();\n\n// 计算差值并转换为天数\nvar diff_ms = now_ms - entity_date_ms;\nvar diff_days = Math.floor(diff_ms / (1000 * 60 * 60 * 24));\n\n// 返回差值\nmsg.payload = -diff_days;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":1580,"wires":[["d7d64e7ae6af147b"]]},{"id":"d7d64e7ae6af147b","type":"ha-sensor","z":"6ce0f0465744cfa4","name":"","entityConfig":"280daa7788261365","version":0,"state":"payload","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":630,"y":1580,"wires":[[]]},{"id":"9b9f41a8.34d99","type":"server","name":"我的家","version":5,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false,"heartbeat":true,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":false},{"id":"280daa7788261365","type":"ha-entity-config","server":"9b9f41a8.34d99","deviceConfig":"","name":"red-letter day","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"red-letter day"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"天"},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false}]
复制代码导入nodered

卡片代码:
微信图片_20240124092341.png
使用代码添加的这三处是不用修改的,可以直接用

top.zip (7.91 KB, 下载次数: 95)
回复

使用道具 举报

12

主题

434

帖子

2340

积分

金牌会员

Rank: 6Rank: 6

积分
2340
金钱
1906
HASS币
0
发表于 2024-1-24 10:01:52 | 显示全部楼层
holiday就是添加删除纪念日不方便,没有前端
回复

使用道具 举报

5

主题

44

帖子

726

积分

高级会员

Rank: 4

积分
726
金钱
682
HASS币
0
发表于 2024-1-24 11:06:15 | 显示全部楼层
楼主统计 有多少灯打开   有多少插座打开的那个用什么实现的
回复

使用道具 举报

2

主题

116

帖子

1918

积分

金牌会员

Rank: 6Rank: 6

积分
1918
金钱
1802
HASS币
0
发表于 2024-1-24 11:09:06 | 显示全部楼层
本帖最后由 chali_bulang 于 2024-1-24 11:10 编辑

人在国内 holiday获取到了"Chinese New Year (Spring Festival)" ,不是中文的"春节"
哈哈哈哈 难道是因为梯子?

回复

使用道具 举报

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24293
金钱
22862
HASS币
290
发表于 2024-1-24 11:35:03 | 显示全部楼层
感谢分享!

010.jpg

这个时间直接用模板也可以:

type: markdown
content: >-
  ### {% if now().hour >= 6 and now().hour < 9 -%}

  早上好,

  {%- elif now().hour >= 9 and now().hour < 12 -%}

  上午好,

  {%- elif now().hour >= 12 and now().hour < 13 -%}

  中午好,

  {%- elif now().hour >= 13 and now().hour < 18 -%}

  下午好,

  {%- elif now().hour >= 18 and now().hour < 23 -%}

  晚上好,

  {%- else -%}

  夜深了,请注意休息哦!

  {%- endif %} {{user}}


  {% set stop_time = as_timestamp(state_attr("calendar.zhong_guo",
  "start_time")) - as_timestamp(now()) %}

  {% set days = (stop_time // (60 * 60 * 24)) %}

  {% set hours = (stop_time// (60 * 60)) - days * 24 %}

  {% set minutes = (stop_time // 60) - (days * 24 * 60) %}

  {% set days = ((stop_time // (60 * 60 * 24))) %}

  {% set minutes = (minutes) - (hours * 60) %}

  {% macro phrase(value, name) %}
    {%- set value = value | int %}
    {{-'{}{}'.format(value,name) if value | int > 0 else''}}
  {%- endmacro %}

  {% set text = [ phrase(days,'天'), phrase(hours,'小时'), phrase(minutes,'分钟') ] |
  select('!=','') | list | join('') %}

  ### 距离{{state_attr("calendar.zhong_guo", "message")}}还有:{{text}}




回复

使用道具 举报

12

主题

434

帖子

2340

积分

金牌会员

Rank: 6Rank: 6

积分
2340
金钱
1906
HASS币
0
发表于 2024-1-24 11:37:49 | 显示全部楼层
同问,统计灯的代码
回复

使用道具 举报

0

主题

215

帖子

2551

积分

金牌会员

Rank: 6Rank: 6

积分
2551
金钱
2336
HASS币
0
发表于 2024-1-24 12:39:10 | 显示全部楼层
mqs000 发表于 2024-1-24 11:06
楼主统计 有多少灯打开   有多少插座打开的那个用什么实现的

将灯和插座分别建群组,以所有灯为例:
type: custom:mushroom-chips-card
  chips:
    - type: menu
    - type: conditional
      conditions:
        - entity: light.suo_you_deng
          state: 'on'
      chip:
        type: template
        icon: mdi:lightbulb
        content: |-
          {{ expand('light.suo_you_deng') | selectattr( 'state', 'eq',
          'on') | list | count }}
        entity: light.suo_you_deng
        icon_color: orange
回复

使用道具 举报

12

主题

156

帖子

1351

积分

金牌会员

Rank: 6Rank: 6

积分
1351
金钱
1195
HASS币
10
发表于 2024-1-24 15:59:40 | 显示全部楼层
chali_bulang 发表于 2024-1-24 11:09
人在国内 holiday获取到了"Chinese New Year (Spring Festival)" ,不是中文的"春节"
哈哈哈哈 难道是因为 ...

同款+1?  什么问题?
回复

使用道具 举报

0

主题

100

帖子

933

积分

高级会员

Rank: 4

积分
933
金钱
833
HASS币
0
发表于 2024-1-24 22:28:46 | 显示全部楼层
学习了。
回复

使用道具 举报

5

主题

82

帖子

1293

积分

金牌会员

Rank: 6Rank: 6

积分
1293
金钱
1211
HASS币
0
发表于 2024-1-24 23:28:12 | 显示全部楼层
多谢分享
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-23 05:41 , Processed in 0.090497 second(s), 34 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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