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

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

[已解决] 自动化条件问题-距上一次设备关闭时间小于时长条件

[复制链接]

1

主题

20

帖子

132

积分

注册会员

Rank: 2

积分
132
金钱
112
HASS币
0
发表于 2024-2-20 08:45:32 | 显示全部楼层 |阅读模式
本帖最后由 jk_1 于 2024-2-21 17:53 编辑

1. 触发条件:设备开启
2. 环境条件:跟上次关闭时间小于1小时(该条件不知道怎么写 胡乱试过下面写法也不对:  {{ now() - states('media_player.sony_xr_75x90j').off.as_timestamp() < 3600 }}
3. 执行:关闭设备
trigger:
  - platform: device
    type: turned_on
    device_id: 3c90eeec950ca2125d5cade55b543eb0
    entity_id: e9f8f574eda028193e5cbb720a6b4119
    domain: remote
    for:
      hours: 0
      minutes: 0
      seconds: 6
condition:
  - condition: and
    conditions:
      - condition: time
        after: "08:00:00"
        before: "17:00:00"
      - condition: template
        value_template: " {{ now() - states('media_player.sony_xr_75x90j').off.as_timestamp() < 3600 }}"
action:
  - type: turn_off
    device_id: 3c90eeec950ca2125d5cade55b543eb0
    entity_id: e9f8f574eda028193e5cbb720a6b4119
    domain: remote


求助!

贴一下最后的。感谢 Rogercjnt007
alias: sony_lastoff
description: ""
trigger:
  - platform: device
    device_id: 3c90eeec950ca2125d5cade55b543eb0
    domain: media_player
    entity_id: de4edf74613e55cd80e9a8fdc88b182e
    type: turned_off
condition: []
action: []
trigger:
  - platform: device
    type: turned_on
    device_id: 3c90eeec950ca2125d5cade55b543eb0
    entity_id: e9f8f574eda028193e5cbb720a6b4119
    domain: remote
    for:
      hours: 0
      minutes: 0
      seconds: 6
condition:
  - condition: time
    after: "08:00:00"
    before: "17:00:00"
  - condition: template
    value_template: >-
      {{
      (as_timestamp(now())-as_timestamp(states.automation.sony_lastoff.last_updated))
      < 1800 }}
action:
  - type: turn_off
    device_id: 3c90eeec950ca2125d5cade55b543eb0
    entity_id: e9f8f574eda028193e5cbb720a6b4119
    domain: remote
- delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: xiaomi_miot.intelligent_speaker
    data:
      entity_id: media_player.xiaomi_l05c_2c30_play_control
      text: 请让电视机休息一会吧
      execute: false





回复

使用道具 举报

6

主题

275

帖子

3943

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3943
金钱
3663
HASS币
0
发表于 2024-2-20 09:29:53 | 显示全部楼层
condition:
    condition: and
    conditions:
    - condition: template
      value_template: '{{ (as_timestamp(now())-as_timestamp(states.media_player.sony_xr_75x90j.last_updated)) < 3600 }}'      
    - condition: state
      entity_id: media_player.sony_xr_75x90j
      state: 'off'
回复

使用道具 举报

0

主题

210

帖子

2187

积分

金牌会员

Rank: 6Rank: 6

积分
2187
金钱
1977
HASS币
0
发表于 2024-2-20 09:34:51 | 显示全部楼层
as_timestamp(trigger.event.data.new_state.last_changed) as_timestamp(trigger.event.data.old_state.last_changed)
回复

使用道具 举报

1

主题

20

帖子

132

积分

注册会员

Rank: 2

积分
132
金钱
112
HASS币
0
 楼主| 发表于 2024-2-20 12:21:13 | 显示全部楼层

代码关于判断时长的语句可行
这里有个小问题:
1.现在电视打开了
2.我需要判断打开时间或现在时间距上次关闭的时长
回复

使用道具 举报

15

主题

305

帖子

2247

积分

金牌会员

Rank: 6Rank: 6

积分
2247
金钱
1942
HASS币
0
发表于 2024-2-20 12:40:47 | 显示全部楼层
提供个笨办法:添加个日期时间辅助元素,另建一个自动化,当media_player.sony_xr_75x90j关闭时,把时间记录下来,再在以上的自动化里进行判断
回复

使用道具 举报

1

主题

20

帖子

132

积分

注册会员

Rank: 2

积分
132
金钱
112
HASS币
0
 楼主| 发表于 2024-2-20 12:46:37 | 显示全部楼层
二楼大神的代码似乎是可行的,我再测试测试
回复

使用道具 举报

6

主题

275

帖子

3943

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3943
金钱
3663
HASS币
0
发表于 2024-2-20 15:15:05 | 显示全部楼层
jk_1 发表于 2024-2-20 12:46
二楼大神的代码似乎是可行的,我再测试测试

放心去用吧,不用谢
回复

使用道具 举报

1

主题

20

帖子

132

积分

注册会员

Rank: 2

积分
132
金钱
112
HASS币
0
 楼主| 发表于 2024-2-21 16:39:56 | 显示全部楼层
cjnt007 发表于 2024-2-20 12:40
提供个笨办法:添加个日期时间辅助元素,另建一个自动化,当media_player.sony_xr_75x90j关闭时,把时间记 ...

用该方法,已成功。多谢!
回复

使用道具 举报

1

主题

20

帖子

132

积分

注册会员

Rank: 2

积分
132
金钱
112
HASS币
0
 楼主| 发表于 2024-2-21 16:47:40 | 显示全部楼层
本帖最后由 jk_1 于 2024-2-21 18:06 编辑
jk_1 发表于 2024-2-20 12:46
二楼大神的代码似乎是可行的,我再测试测试

看了下,好像还是不太成
微信截图_20240221180415.png
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-2 08:37 , Processed in 0.631310 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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