# logik
# hvis om dagen, så høj lydstyrke. Gem sonos-config. udkoble alle. Ding-dong i lille bad og hund der gør i alrum. Blink med lamperne.
# send push besked til telefoner


- id: '1672567487970'
  alias: Ringklokke fordør
  description: Når det ringer på fordør, send besked, lav lyd og blink
  trigger:
  - platform: mqtt
    topic: '{{''shellies/shellybutton1-'' ~ button_id ~ ''/input_event/0''}}'
  condition: []
  action:
  # Gem gammel og ny event_cnt
  - variables:
      new_cnt: "{{ trigger.payload_json.event_cnt | int(0) }}"
      old_cnt: "{{ states('input_number.shelly_fordor_event_cnt') | int(-1) }}"
  # Opdater counter ALTID (ogsaa ved wake-up)
  - service: input_number.set_value
    target:
      entity_id: input_number.shelly_fordor_event_cnt
    data:
      value: "{{ new_cnt }}"
  # Log ALLE MQTT events (ogsaa filtrerede) til logbog for debugging
  - service: logbook.log
    data:
      name: "Shelly fordoer"
      message: >-
        MQTT event={{ trigger.payload_json.event }}
        event_cnt={{ new_cnt }} (old={{ old_cnt }})
        battery={{ trigger.payload_json.battery | default('?') }}%
      entity_id: input_number.shelly_fordor_event_cnt
  # Stop hvis: foerste sync efter restart (old=-1) eller samme cnt (periodisk wake-up)
  - condition: template
    value_template: "{{ old_cnt >= 0 and new_cnt != old_cnt }}"
  # Bloker ringeklokke om natten (22-06)
  - condition: time
    after: '06:00:00'
    before: '22:00:00'
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.payload_json.event == "S" }}'
      sequence:
      - parallel:
        - sequence:
          - choose:
            - conditions:
              - condition: time
                after: '20:00:00'
                before: '06:00:00'
              sequence:
              - service: media_player.volume_set
                data:
                  volume_level: 0.5
                target:
                  entity_id: media_player.lille_badevaerelse
              - service: media_player.play_media
                target:
                  entity_id: media_player.lille_badevaerelse
                data:
                  media_content_id: media-source://media_source/local/Halloween-doorbell.mp3
                  media_content_type: audio/mpeg
                metadata:
                  title: Halloween-doorbell.mp3
                  thumbnail:
                  media_class: music
                  children_media_class:
                  navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://media_source
            default:
            - service: media_player.volume_set
              data:
                volume_level: 0.8
              target:
                entity_id: media_player.lille_badevaerelse
            - service: media_player.play_media
              target:
                entity_id: media_player.lille_badevaerelse
              data:
                media_content_id: media-source://media_source/local/doorbell.mp3
                media_content_type: audio/mpeg
              metadata:
                title: doorbell.mp3
                thumbnail:
                media_class: music
                children_media_class:
                navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://media_source
        - sequence:
          - service: tts.speak
            target:
              entity_id: tts.google_ai_tts
            data:
              media_player_entity_id: media_player.alrum
              message: "Der er nogen der ringer på fordøren"
        - sequence:
          - service: notify.mobile_app_claus_iphone_15pro
            data:
              message: Det ringer på fordøren
          - service: notify.mobile_app_annes_iphone_14_pro
            data:
              message: Det ringer på fordøren
        - sequence:
          - service: scene.create
            data:
              scene_id: before
              snapshot_entities:
              - light.pendle1
          - service: light.turn_on
            data:
              entity_id: light.pendle1
              brightness: 255
              rgb_color: [255, 0, 0]
              flash: long
          - delay: 2
          - service: scene.turn_on
            entity_id: scene.before
            
    - conditions:
      - condition: template
        value_template: '{{ trigger.payload_json.event == "SS" }}'
      sequence:
      - parallel:
        - sequence:
          - choose:
            - conditions:
              - condition: time
                after: '20:00:00'
                before: '06:00:00'
              sequence:
              - service: media_player.volume_set
                data:
                  volume_level: 0.5
                target:
                  entity_id: media_player.lille_badevaerelse
              - service: media_player.play_media
                target:
                  entity_id: media_player.lille_badevaerelse
                data:
                  media_content_id: media-source://media_source/local/german-shephard.mp3
                  media_content_type: audio/mpeg
                metadata:
                  title: german-shephard.mp3
                  thumbnail:
                  media_class: music
                  children_media_class:
                  navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://media_source
            default:
            - service: media_player.volume_set
              data:
                volume_level: 0.8
              target:
                entity_id: media_player.lille_badevaerelse
            - service: media_player.play_media
              target:
                entity_id: media_player.lille_badevaerelse
              data:
                media_content_id: media-source://media_source/local/german-shephard.mp3
                media_content_type: audio/mpeg
              metadata:
                title: german-shephard.mp3
                thumbnail:
                media_class: music
                children_media_class:
                navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://media_source
        - sequence:
          - service: tts.speak
            target:
              entity_id: tts.google_ai_tts
            data:
              media_player_entity_id: media_player.alrum
              message: "Der er nogen der ringer på fordøren"
        - sequence:
          - service: notify.mobile_app_claus_iphone_15pro
            data:
              message: Det ringer på fordøren - doublepress
          - service: notify.mobile_app_annes_iphone_14_pro
            data:
              message: Det ringer på fordøren - doublepress

    - conditions:
      - condition: template
        value_template: '{{ trigger.payload_json.event == "SSS" }}'
      sequence:
      - parallel:
        - sequence:
          - choose:
            - conditions:
              - condition: time
                after: '20:00:00'
                before: '06:00:00'
              sequence:
              - service: media_player.volume_set
                data:
                  volume_level: 0.5
                target:
                  entity_id: media_player.lille_badevaerelse
              - service: media_player.play_media
                target:
                  entity_id: media_player.lille_badevaerelse
                data:
                  media_content_id: media-source://media_source/local/dog-barking-2-bullmastiff.mp3
                  media_content_type: audio/mpeg
                metadata:
                  title: dog-barking-2-bullmastiff.mp3
                  thumbnail:
                  media_class: music
                  children_media_class:
                  navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://media_source
            default:
            - service: media_player.volume_set
              data:
                volume_level: 0.8
              target:
                entity_id: media_player.lille_badevaerelse
            - service: media_player.play_media
              target:
                entity_id: media_player.lille_badevaerelse
              data:
                media_content_id: media-source://media_source/local/dog-barking-2-bullmastiff.mp3
                media_content_type: audio/mpeg
              metadata:
                title: dog-barking-2-bullmastiff.mp3
                thumbnail:
                media_class: music
                children_media_class:
                navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://media_source
        - sequence:
          - service: tts.speak
            target:
              entity_id: tts.google_ai_tts
            data:
              media_player_entity_id: media_player.alrum
              message: "Der er nogen der ringer på fordøren"
        - sequence:
          - service: notify.mobile_app_claus_iphone_15pro
            data:
              message: Det ringer på fordøren - triple-press
          - service: notify.mobile_app_annes_iphone_14_pro
            data:
              message: Det ringer på fordøren - triple-press

    - conditions:
      - condition: template
        value_template: '{{ trigger.payload_json.event == "L" }}'
      sequence:
      - parallel:
        - sequence:
          - choose:
            - conditions:
              - condition: time
                after: '20:00:00'
                before: '06:00:00'
              sequence:
              - service: media_player.volume_set
                data:
                  volume_level: 0.5
                target:
                  entity_id: media_player.lille_badevaerelse
              - service: media_player.play_media
                target:
                  entity_id: media_player.lille_badevaerelse
                data:
                  media_content_id: media-source://media_source/local/two-tone-chime.mp3
                  media_content_type: audio/mpeg
                metadata:
                  title: two-tone-chime.mp3
                  thumbnail:
                  media_class: music
                  children_media_class:
                  navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://media_source
            default:
            - service: media_player.volume_set
              data:
                volume_level: 0.8
              target:
                entity_id: media_player.lille_badevaerelse
            - service: media_player.play_media
              target:
                entity_id: media_player.lille_badevaerelse
              data:
                media_content_id: media-source://media_source/local/two-tone-chime.mp3
                media_content_type: audio/mpeg
              metadata:
                title: two-tone-chime.mp3
                thumbnail:
                media_class: music
                children_media_class:
                navigateIds:
                - {}
                - media_content_type: app
                  media_content_id: media-source://media_source
        - sequence:
          - service: tts.speak
            target:
              entity_id: tts.google_ai_tts
            data:
              media_player_entity_id: media_player.alrum
              message: "Der er nogen der ringer på fordøren"
        - sequence:
          - service: notify.mobile_app_claus_iphone_15pro
            data:
              message: Det ringer på fordøren - long-press
          - service: notify.mobile_app_annes_iphone_14_pro
            data:
              message: Det ringer på fordøren - long-press

  mode: single
  trigger_variables:
    button_id: C45BBE6A5696
