Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否增加按月计算的阶梯电价 #30

Open
EricCorleone opened this issue Jul 9, 2024 · 9 comments
Open

能否增加按月计算的阶梯电价 #30

EricCorleone opened this issue Jul 9, 2024 · 9 comments

Comments

@EricCorleone
Copy link

有的地区的阶梯电价是按月来的而不是按年,每个月都会重置,目前的方法无法简单地将年电量实体替换成月电量来实现,因为月电量统计的是上个月的用电量。是否会考虑新增类似年用电量那样的实时累计的本月用电量实体?

@Tuisku-L
Copy link
Contributor

其实挺简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里拿出来本月 1 号到今天的用电量合计,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。
我自己瞎写了一下,就不往这里合并了,你可以参考一下:
Tuisku-L@bb0f457

@EricCorleone
Copy link
Author

EricCorleone commented Jul 10, 2024

很强,我去研究一下

@lixinzai
Copy link

其实挺简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里拿出来本月 1 号到今天的用电量合计,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。 我自己瞎写了一下,就不往这里合并了,你可以参考一下: Tuisku-L@bb0f457

请问下实体的哪个怎么弄呀

@Tuisku-L
Copy link
Contributor

其实挺简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里拿出来本月 1 号到今天的用电量合计,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。 我自己瞎写了一下,就不往这里合并了,你可以参考一下: Tuisku-L@bb0f457

请问下实体的哪个怎么弄呀

在 HA configuration.yaml 的 template 下添加实体就可以了,比如我加的某两个:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_usage_db
    sensor:
      - name: 本月累计用电量
        unique_id: month_electricity_usage_db
        state: "{{ states('sensor.month_electricity_usage_db') }}"
        attributes:
          present_date: "{{ state_attr('sensor.month_electricity_usage_db', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.month_electricity_usage_db', 'last_updated') }}"
        state_class: total_increasing
        unit_of_measurement: "kWh"
        device_class: energy

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_charge_db
    sensor:
      - name: 本月累计电费
        unique_id: month_electricity_charge_db
        state: "{{ states('sensor.month_electricity_charge_db') }}"
        state_class: total_increasing
        unit_of_measurement: "CNY"
        device_class: monetary

对应的更新实体的代码在我的分支上有,可以根据关键字搜一下

@lixinzai
Copy link

其实很简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里来本月 1 号到今天的用电量,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。我自己瞎写了一下,就不往这里合并了,你可以参考一下:Tuisku-L@bb0f457

好奇下实体的哪个怎么弄呀

在HA配置.yaml的模板下添加实体就可以了,比如我加的一些:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_usage_db
    sensor:
      - name: 本月累计用电量
        unique_id: month_electricity_usage_db
        state: "{{ states('sensor.month_electricity_usage_db') }}"
        attributes:
          present_date: "{{ state_attr('sensor.month_electricity_usage_db', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.month_electricity_usage_db', 'last_updated') }}"
        state_class: total_increasing
        unit_of_measurement: "kWh"
        device_class: energy

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_charge_db
    sensor:
      - name: 本月累计电费
        unique_id: month_electricity_charge_db
        state: "{{ states('sensor.month_electricity_charge_db') }}"
        state_class: total_increasing
        unit_of_measurement: "CNY"
        device_class: monetary

对应的更新实体的代码在我的分支上有,可以根据关键字搜一下

好的,能源面板用last_electricity_usage的你会刷新数据出来吗,我这边只会显示0

@Tuisku-L
Copy link
Contributor

其实很简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里来本月 1 号到今天的用电量,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。我自己瞎写了一下,就不往这里合并了,你可以参考一下:Tuisku-L@bb0f457

好奇下实体的哪个怎么弄呀

在HA配置.yaml的模板下添加实体就可以了,比如我加的一些:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_usage_db
    sensor:
      - name: 本月累计用电量
        unique_id: month_electricity_usage_db
        state: "{{ states('sensor.month_electricity_usage_db') }}"
        attributes:
          present_date: "{{ state_attr('sensor.month_electricity_usage_db', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.month_electricity_usage_db', 'last_updated') }}"
        state_class: total_increasing
        unit_of_measurement: "kWh"
        device_class: energy

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_charge_db
    sensor:
      - name: 本月累计电费
        unique_id: month_electricity_charge_db
        state: "{{ states('sensor.month_electricity_charge_db') }}"
        state_class: total_increasing
        unit_of_measurement: "CNY"
        device_class: monetary

对应的更新实体的代码在我的分支上有,可以根据关键字搜一下

好的,能源面板用last_electricity_usage的你会刷新数据出来吗,我这边只会显示0

这个不太确定,我没用 HA 的能源面板,我是自定义了几个卡片显示了信息;我感觉能源面板不适合社会主义中国(……),现在我们能拿到的数据精细化程度满足不了能源面板的需求;

不过我看 last_electricity_usage 可能缺少个 last_reset 的属性,你可以试下改成:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.last_electricity_usage
    sensor:
      - name: 最近一天用电量
        unique_id: last_electricity_usage_entity
        state: "{{ states('sensor.last_electricity_usage') }}"
        attributes:
          present_date: "{{ state_attr('sensor.last_electricity_usage', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.last_electricity_usage', 'last_updated') }}"
          last_reset: "{{ now().strftime('%Y-%m-%dT00:00:00+00:00') if now().hour == 0 and now().minute == 0 else state_attr('sensor.last_electricity_usage_entity', 'last_reset') }}"
        state_class: total
        unit_of_measurement: "kWh"
        device_class: energy

不确定行不行(

@lixinzai
Copy link

其实很简单的,添加几个实体(比如本月用电量、每月电费),然后从数据库里来本月1号到今天的用电量,写一个计算阶梯价格的函数,结果更新到这两个实体就行了。我自己瞎写了一下,不往这里合并了,你可以参考一下:Tuisku-L@bb0f457

好奇下实体的哪个怎么弄呀

在HA配置.yaml的模板下添加实体就可以了,比如我加的一些:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_usage_db
    sensor:
      - name: 本月累计用电量
        unique_id: month_electricity_usage_db
        state: "{{ states('sensor.month_electricity_usage_db') }}"
        attributes:
          present_date: "{{ state_attr('sensor.month_electricity_usage_db', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.month_electricity_usage_db', 'last_updated') }}"
        state_class: total_increasing
        unit_of_measurement: "kWh"
        device_class: energy

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.month_electricity_charge_db
    sensor:
      - name: 本月累计电费
        unique_id: month_electricity_charge_db
        state: "{{ states('sensor.month_electricity_charge_db') }}"
        state_class: total_increasing
        unit_of_measurement: "CNY"
        device_class: monetary

对应的更新实体的代码在我的分支上有,可以根据关键字搜一下

好的,能源面板用last_electricity_usage的你会刷新数据出来吗,我的演讲只显示0

这个不确定,我没用HA的能源面板,我是自定义了几个反馈显示了信息;我感觉能源面板不适合社会主义中国(……),现在我们能够获得的数据精细化程度满足无法能源面板的需求;

我看last_electricity_usage可能缺少一个last_reset属性,你可以尝试下改成:

  - trigger:
      - platform: event
        event_type: "state_changed"
        event_data:
          entity_id: sensor.last_electricity_usage
    sensor:
      - name: 最近一天用电量
        unique_id: last_electricity_usage_entity
        state: "{{ states('sensor.last_electricity_usage') }}"
        attributes:
          present_date: "{{ state_attr('sensor.last_electricity_usage', 'present_date') }}"
          last_updated: "{{ state_attr('sensor.last_electricity_usage', 'last_updated') }}"
          last_reset: "{{ now().strftime('%Y-%m-%dT00:00:00+00:00') if now().hour == 0 and now().minute == 0 else state_attr('sensor.last_electricity_usage_entity', 'last_reset') }}"
        state_class: total
        unit_of_measurement: "kWh"
        device_class: energy

不确定行不行(

不行 用reset的话得出的是今天减昨天的,能源就显示个位数了

@EricCorleone
Copy link
Author

EricCorleone commented Jul 29, 2024

其实挺简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里拿出来本月 1 号到今天的用电量合计,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。 我自己瞎写了一下,就不往这里合并了,你可以参考一下: Tuisku-L@bb0f457

感谢,我按你说的搞定了实时月用电量,但是实时月电费是没法实现了,因为我家开通了峰谷用电,以现有的数据是没法计算每天的电价的,比较可惜。还有你代码里计算的上月用电量似乎并没有必要,原代码里的sensor.month_electricity_usage就是上个月的用电量

@Tuisku-L
Copy link
Contributor

其实挺简单的,添加几个实体(比如本月用电量、本月电费),然后从数据库里拿出来本月 1 号到今天的用电量合计,写一个计算阶梯价格的函数,结果更新到这俩实体就行了。 我自己瞎写了一下,就不往这里合并了,你可以参考一下: Tuisku-L@bb0f457

我按你说的搞定了实时月用电量,但是实时月电费是没法实现了,因为我家开通了峰谷用电,以现有的数据是没法计算每天的电价的,比较可惜

应该也可以,电网的数据好像是有给峰谷电的,但是脚本里没去爬,要修改的更深一点了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants